题
JQuery似乎在尝试传递像这样的字母数字参数时出错:
$.ajax({
type: "POST",
url: "Default.aspx/AjaxTest",
data: "{eventID:9a5}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
},
error: function(e) {
alert("Event could not be added to calendar");
}
});
调用上述方法时,将调用错误回调。但是,当我将eventID参数更改为纯数字值时,它工作正常,并调用成功回调。我想将一个字母数字值传递给服务器方法,这似乎不起作用。任何帮助将不胜感激。
艾哈迈德
解决方案 2
我刚刚学会了如何解决这个问题。事实证明我收到了一条JSON错误消息:“无效的JSON原语”。我不得不在我的字符串参数周围添加额外的单引号,以便JSON在将其反序列化时理解它是一个字符串。我在我的字母数字数据周围添加了单引号,以便JSON理解它是一个字符串。这就是我的代码最终工作的方式:
$.ajax({
type: "POST",
url: "Default.aspx/AjaxTest",
data: "{eventID:'9a5'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
},
error: function(e) {
alert("Event could not be added to calendar");
}
});
无论如何,谢谢大家。 其他提示
你不应该像普通的Javascript字典一样传递数据吗?
$.ajax({
...
data: {"eventID": "9a5", "SomeNumericField": 25}
...
});
(例如:不要在数据周围加上引号。我很确定它不应该是那样的字符串。)
不隶属于 StackOverflow