使用jQuery.Ajax()将对象传递给服务器的问题

时间:2010-02-16 10:42:49

标签: asp.net jquery ajax

我正在尝试实现this question

中提到的相同代码

目前我有以下代码:

var pagePath = window.location.pathname;

var paramList = '';
if (paramArray.length > 0) {
 for (var i = 0; i < paramArray.length; i ++) {
  if (paramList.length > 0) paramList += ',';
  paramList += "{'id':'" + paramArray[i].id + "', 
                       'collapsed':'" + paramArray[i].collapsed + "', 
                       'order':'" + paramArray[i].order + "', 
                       'column':'" + paramArray[i].column + "'}";
 }
}
paramList = '[' + paramList + ']';

$.ajaxSetup({ cache: false });
//Call the page method  
$.ajax({
 type: "POST",
 url: pagePath + "/" + fn,
 contentType: "application/json; charset=utf-8",
 data: "{'items': **'**" + $.toJSON(paramList) + "**'**}",

dataType:“json”,  成功:成功,  错误:errorFn  });

我正在尝试将此数据传递给WebMethod

[WebMethod]
public static String SaveData(Dictionary<String, Object>[] items)

问题是我一直收到错误“500 Internal Server Error”。 我很确定数据类型是导致问题但却无法解决的问题。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您的服务是否使用[ScriptService]属性修饰?

星号是什么?这是一个新的宏我没有收到备忘录吗?

"{'items': **'**" + $.toJSON(paramList) + "**'**}"

“{'items':”+ $ .toJSON(paramList)+“}”

正如我所看到的,你已经在创造了json。不需要toJSON

尝试用双引号替换单引号。 e.g。

'{"items": ' + paramList + '}'

您的帖子数据应如下所示

'{
    "items": [{
        "id": "1",
        "collapsed": "0",
        "order": "0",
        "column": "column2"
    },
    {
        "id": "2",
        "collapsed": "1",
        "order": "1",
        "column": "column2"
    },
    {
        "id": "3",
        "collapsed": "0",
        "order": "0",
        "column": "column3"
    }]
}'

内联

'{ "items": [{ "id": "1", "collapsed": "0", "order": "0", "column": "column2" }, { "id": "2", "collapsed": "1", "order": "1", "column": "column2" }, { "id": "3", "collapsed": "0", "order": "0", "column": "column3" }]}'



var paramList = "";
if (paramArray.length > 0) {
 for (var i = 0; i < paramArray.length; i ++) {
  if (paramList.length > 0) paramList += ",";
  paramList += '{"id":"' + paramArray[i].id + '", 
                       "collapsed":"' + paramArray[i].collapsed + '", 
                       "order":"' + paramArray[i].order + '", 
                       "column":"' + paramArray[i].column + '"}';
 }
}
paramList = "[" + paramList + "]";

$.ajaxSetup({ cache: false });
//Call the page method  
$.ajax({
 type: 'POST',
 url: pagePath + '/' + fn,
 contentType: 'application/json; charset=utf-8',
 data: '{"items":' + paramList + '}',  // the rest of your function is missing