AJAX呼叫C#後臺方法(帶引數)
1、C#程式碼
[WebMethod]
public static int httest(int s)
{
_Default temp = new _Default();
if (s == 123)
return 111;
return temp.dom();
//Response.Write("<script>alert('呼叫成功!')</script>");
}
2、AJAX程式碼
$.ajax({
type: "post", //要用post方式
url: "Default.aspx/httest",//方法所在頁面和方法名
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
data:"{'s':'"+123+"'}",//格式為:"{'name':'value'}"
success: function (data) {
// console.log(data.d);//返回的資料用data.d獲取內容
num=data.d;
},
error: function (err) {
alert(err);
}
});