Ajax与asp.net一起使用的规则很少。
- 您的WebMethod应该为
public和static。- 如果您的WebMethod期望某些参数,则必须像
data在ajax中一样传递这些参数。- 参数名称应
same在ajax中,WebMethod且应在data其中。- 从ajax传递的数据应该在中
jsonstring。为此,您可以使用,JSON.stringify否则您必须将values参数的包围在中quotes。
请检查以下示例ajax调用
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;}编辑
如果
GET在ajax中使用,则需要启用从
GET请求中调用Web方法。
[System.Web.script.Services.scriptMethod(UseHttpGet= true)]在WebMetod上添加
[System.Web.Services.WebMethod][System.Web.script.Services.scriptMethod(UseHttpGet = true)]public static int ItemCount()



