gpt4 book ai didi

javascript - PageMethods 未定义错误

转载 作者:行者123 更新时间:2023-12-03 05:55:03 25 4
gpt4 key购买 nike

我有以下方法背后的代码,我想使用 JScript 调用它

VB代码

   <WebMethod>
Public Shared Function SayHello() As String
Return ("Hello JScript")
End Function

ASPX

<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>

只有我得到:未捕获的ReferenceError:PageMethods未定义

我正在尝试解决这个问题,但没有办法,需要帮助。

最佳答案

您在标记中错过了 Microsoft Ajax 扩展。

<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 ajax 扩展):

您需要 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");
}
});
}

关于javascript - PageMethods 未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39972067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com