I was creating a simple web method to access from Java script..But I am not able to
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<s开发者_高级运维cript src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(
function test() {
var x = PageMethods.MyMethod();
alert(x.toString());
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" EnablePartialRendering="true">
</asp:ScriptManager>
</div>
</form>
</body>
</html>
Code behind looks like this
[WebMethod]
public static string MyMethod()
{
return "Hello";
}
Variable x is null. Iam not able to figure out what is is that Iam missing ?Any help ? Thanks in Advance
You will need to define a callback function which is invoked upon reciept of the response from the web method:
$(document).ready(
function test() {
PageMethods.MyMethod(myMethodCallBackSuccess, myMethodCallBackFailed);
})
function myMethodCallBackSuccess(response) {
alert(response);
}
function myMethodCallBackFailed(error) {
alert(error.get_message());
}
You may also pass arguments to the method however these must always come before the success and failure callbacks.
Note: you do not need to include the failed call back but it is available if required.
精彩评论