jquery - calling server function from client side -
i need call method in code behind client side using json, method never got called, , error "c" blank. did 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; }
if want call method in asp page, you're going need add logic inside asp page call function. can't call directly $.ajax(). example, ajax call might be:
$.ajax({ type: "post", contenttype: "application/json; charset=utf-8", url: "mypage.aspx", data: { item: item, method: 'checkitem' }, datatype: "json", success: function (result) { if (result) { errormessage.innerhtml = 'warning: item exists.'; return false; } }, error: function (a,b,c) { alert("error: " + c); } });
and inside asp code, "method" form variable , call specified method.
Comments
Post a Comment