Вот как можно вызвать пользовательский ASP.NET PageMethod с помощью jQuery и Microsoft Ajax:
<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript" src="http://www.json.org/json2.js"></script>
</head>
<script runat="server">
/// <summary>
/// A custom PageMethod that will echo back the argument
/// </summary>
/// <param name="argument">some arbitrary string</param>
/// <returns></returns>
[System.Web.Services.WebMethod]
public static string MyPageMethod(string argument)
{
return "hello " + argument;
}
</script>
<script type="text/javascript">
function callPageMthodWithMicrosoftAjax(argument) {
PageMethods.MyPageMethod(argument, function(result) {
document.getElementById('result').innerHTML = result;
}, function(error) {
alert(error.get_message());
});
}
function callPageMthodWithjQueryAjax(argument) {
jQuery.ajax({
url: '/default.aspx/MyPageMethod',
type: 'post',
contentType: 'application/json',
data: JSON.stringify({ argument: argument }),
dataType: 'json',
processData: false,
success: function(data, textStatus) {
jQuery('#result').html(data.d);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
var jsonError = JSON.parse(XMLHttpRequest.responseText);
alert(jsonError.Message);
}
});
}
</script>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="true" />
<a href="#" onclick="callPageMthodWithMicrosoftAjax('Microsoft Ajax')">call PageMethod with Microsoft Ajax</a>
<br />
<a href="#" onclick="callPageMthodWithjQueryAjax('jQuery')">call PageMethod with jQuery</a>
<div id="result"></div>
</form>
</body>
</html>