There are few rules for ajax to work with asp.net.
- Your WebMethod should be
publicandstatic.- If your WebMethod expects some parameter(s) than these parameter(s) must
be passed asdatain ajax.- Name of parameter(s) should be
sameinWebMethodand indatapart
of ajax.- Data passed from ajax should be in
json string.For this you can useJSON.stringifyor you will have to surround thevaluesof parameter(s)
inquotes.
Please check the below sample ajax call
function CallAjax() { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "Default.aspx/CallAjax", data: JSON.stringify({ name: "Mairaj", value: "12" }), dataType: "json", async: false, success: function (data) { //your pre }, error: function (err) { alert(err.responseText); } }); }[WebMethod]public static List<string> CallAjax(string name,int value){ List<string> list = new List<string>(); try { list.Add("Mairaj"); list.Add("Ahmad"); list.Add("Minhas"); } catch (Exception ex) { } return list;}
EDIT
If you use
GETin ajax than you need to enable your webmethod to be called
from
GETrequest. Add
[System.Web.script.Services.scriptMethod(UseHttpGet =true)]on top of WebMetod
[System.Web.Services.WebMethod][System.Web.script.Services.scriptMethod(UseHttpGet = true)]public static int ItemCount()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)