jQuery AJAX实现调用页面后台方法

<script type="text/javascript">
function doSend() {
$.ajax({
     type: "post",
     url: "aaa.aspx/SendMes",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {

    //返回的数据用data.d获取内容
    alert(data.d);
   }
});
}
</script>

后台

[WebMethod] 
public static string SendMes() 

return "Hello Ajax!"; 
}

有参数

$(function() { 
$("#btnOK").click(function() { 
$.ajax({ 
type: "Post", 
url: "demo.aspx/GetStr", 
//方法传参的写法一定要对,str为形参的名字,str2为第二个形参的名字 
data: "{'str':'我是','str2':'XXX'}", 
contentType: "application/json; charset=utf-8", 
dataType: "json", 
success: function(data) { 
//返回的数据用data.d获取内容 
alert(data.d); 
}, 
error: function(err) { 
alert(err); 

}); 

//禁用按钮的提交 
return false; 
}); 
});

[WebMethod] 
public static string GetStr(string str, string str2) 

return str + str2; 
}

原文地址:https://www.cnblogs.com/codeloves/p/2961004.html