方法后面有下面的代码,我想使用JScript调用它
VB代码
<WebMethod>
Public Shared Function SayHello() As String
Return ("Hello JScript")
End FunctionASPX
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function GetMessage() {
var i = PageMethods.SayHello();
document.write(i);
}
setInterval(GetMessage, 500);
</script>
</head>
<body>
</body>
</html>只有我得到:Uncaught : PageMethods没有定义
我正试图解决这个问题,但没有办法,需要帮助,拜托。
发布于 2016-10-11 07:19:08
您在标记中漏掉了Microsoft扩展。
<asp:ScriptManager ID="ScriptManager1"
EnablePageMethods="true"
EnablePartialRendering="true" runat="server" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function GetMessage() {
PageMethods.SayHello(callBack);
}
function callBack(result, userContext, methodName){
alert(result);
}
setInterval(GetMessage, 500);
</script>
</head>
<body>
</body>
</html>尽管这是一种有效的方法,但我更喜欢使用jQuery调用页面方法(MS扩展是不必要的):
您需要jQuery库:
<script src="http://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
function GetMessage() {
$.ajax({
type: "POST",
url: "PageMethods.aspx/SayHello",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
alert(response.d);
},
failure: function(response) {
alert("Error");
}
});
}https://stackoverflow.com/questions/39972067
复制相似问题