I need to call a method in the code behind from client side using json, but the method never got called, and the error "c" is blank. What did I do wrong here?
Client side code:
$.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);
}
});
Server side code:
[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;
}
Kindly enclose your parameter with quotes/double quotes. Please see below.