我正在使用这样的 AJAX 帖子:
$.ajax({
type: "POST",
url: "/AJAXServices.aspx/" + method,
data: params,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
successfunc();
},
error: function(data) {
errorfunc();
}
});
当我像这样使用变量“params”时:
var params = '{"QuestionID":"' + UpdateQuestion_ID + '", "NewText":"' + newText + '"}';
它有效。
但是当我将其更改为:
var params = { QuestionID: UpdateQuestion_ID, NewText: newText };
这会在服务器端引发内部服务器错误:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
服务器端方法:
[WebMethod]
public static void UpdateQuestion(string QuestionID, string NewText)
{
....
}
有什么想法吗?
最佳答案
第一种情况 params
是一个字符串,第二种情况 params
是一个对象。
我猜服务器需要一个有效的 json 字符串。确保 params 是一个对象并使用 JSON.stringify()
将其转换为有效的 JSON发送之前的字符串。
$.ajax({
...
data: JSON.stringify(params),
...
});
关于javascript - JSON 数据作为字符串工作,但在对象时抛出内部错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41312393/