ajax请求aspx.cs后台方法

时间:2022-10-06 14:08:14

前台jquery代码

 $(function () {
$("#btnfix").click(function () {
$.ajax({
type: "post",
url: "fix.aspx/test",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
console.log(data.d);
},
error: function (err) {
alert(err);
}
});
});
});

后台代码

using System.Web.Services; //引用Web.Services命名空间

public partial class Fix : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } [WebMethod] //添加Attribute
public static string test() //方法设为静态
{
return "hello world";
}
}

运行结果

ajax请求aspx.cs后台方法