2016-10-11 78 views
0

我背后方法下面的代码,我想使用的JScriptPageMethods没有定义错误

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> 

只有我叫它得到:Uncaught ReferenceError:PageMethods未定义

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

回答

1

您在标记中嵌入了Microsoft Ajax Extensions。

<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"); 
    } 
}); 
} 
+0

感谢名单anmarti,这是现在工作,但我仍然有错误的结果输出是“未定义”。 –

+0

你使用'scriptManager'或'jQuery'吗? – anmarti

+0

是的我正在使用脚本管理器,,,,现在我得到这个消息:无法读取未定义的属性'ajax'。 –