我需要将JSON数据发送到服务器方法。
当我传递一个简单的'test'字符串,而不是后面的字符串时,此方法有效:
function SendToServer() {
$.ajax({
type: "POST",
url: "Default.aspx/SaveClientGrid",
data: "{ gridData: 'test' }",
contentType: "application/json; charset=utf-8",
dataType: "json"
});
}
不起作用:
function SendToServer() {
var data = JSON.stringify(datasource);
$.ajax({
type: "POST",
url: "Default.aspx/SaveClientGrid",
data: "{ gridData: " + data + " }",
contentType: "application/json; charset=utf-8",
dataType: "json"
});
}
答案 0 :(得分:1)
想一想,这个可行:
function SendToServer() {
var data = JSON.stringify(datasource);
$.ajax({
type: "POST",
url: "Default.aspx/SaveClientGrid",
data: "{ gridData: '" + data + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json"
});
}
我只需在数据周围添加单引号 -
data: "{ gridData: '" + data + "' }"
答案 1 :(得分:-2)
您是否尝试过更改
data: "{ gridData: " + data + " }",
到
data: "{ gridData: ' + data + ' }",
甚至没有报价?也许是一个愚蠢的猜测,但这样的事情总是发生:)