你可以使用一个PageMethod
. 让我们在后面的代码中举例说明这种方法:
[WebMethod]
public static string MyMethod(string memId)
{
return string.Format("Thanks for calling me with id: " + memId);
}
注意事项:方法必须是静态的并用[WebMethod]
属性修饰。
在客户端,您可以使用如下函数调用此方法jQuery.ajax()
:
$.ajax({
url: 'default.aspx/MyMethod',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ memID : mem_ID }),
success: function (result) {
alert(result.d);
}
});
Also the error you are getting about the undefined $
symbol is related to the fact that you didn't reference the jQuery library in your page. So make sure that in your WebForm you have actually added reference to the jQuery library before using it. For example:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>