首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PageMethods未定义错误

PageMethods未定义错误
EN

Stack Overflow用户
提问于 2016-10-11 07:10:42
回答 1查看 6K关注 0票数 0

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

VB代码

代码语言:javascript
复制
   <WebMethod>
    Public Shared Function SayHello() As String
        Return ("Hello JScript")
    End Function

ASPX

代码语言:javascript
复制
<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没有定义

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

EN

回答 1

Stack Overflow用户

发布于 2016-10-11 07:19:08

您在标记中漏掉了Microsoft扩展。

代码语言:javascript
复制
<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库:

代码语言:javascript
复制
<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");
    }
 });
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39972067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档