【发布时间】:2015-01-10 01:12:33
【问题描述】:
我需要使用 json 从客户端调用后面代码中的方法,但该方法从未被调用,并且错误“c”为空白。我在这里做错了什么?
客户端代码:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "MyPage.aspx/CheckItem",
data: {item: item},
dataType: "json",
success: function (result) {
if (result) {
errorMessage.innerHTML = 'WARNING: Item exists.';
return false;
}
},
error: function (a,b,c) {
alert("error: " + c);
}
});
服务器端代码:
[System.Web.Services.WebMethod]
public static bool CheckItem(string item)
{
DataContext dc = new DataContext();
var record = dc.MyTable.Where(x => x.Item == item).FirstOrDefault();
if (record != null)
return true;
else
return false;
}
【问题讨论】:
-
你不应该向
MyPage.aspx/CheckItem而不是MyPage.aspx/MyMethod提出请求吗? -
是的,我做到了(这是一个错字)
-
该代码在您的页面中的什么位置?您确定根本没有调用 ajax 代码吗?
-
我放了一个断点,它转到“错误”,第三个参数(即“c”)为空白
-
尝试在ajax请求中添加
cache: false
标签: jquery asp.net json webmethod