我正在尝试使用jQuery直接调用ASP.NET AJAX页面方法。我正在使用encosia.com作为参考。我的内联javascript是

       <script type="text/javascript">
           $(document).ready(function() {
               // Add the page method call as an onclick handler for the div.
               $("#Result").click(function() {
                   $.ajax({
                       type: "POST",
                       url: "Default.aspx/GetDate",
                       data: "{}",
                       contentType: "application/json; charset=utf-8",
                       dataType: "json",
                       success: function(msg) {
                           // Replace the div's content with the page method's return.
                           $("#Result").text(msg.d);
                       }
                   });
               });
           });

            </script>
       <div id="Result">Click here for the time.</div>


与我的webmethod

<WebMethod()> _
Public Shared Function GetDate() As String
    Return DateTime.Now.ToString()
End Function


我将使用FF并检查已发送的POST,但由于我目前所在的位置只有IE 7,因此这很难做。其他相关信息,ASP.net 2.0。有人知道我在做什么错吗?



更新资料

web.config-预先存在仍无法正常工作

<httpModules>
  <remove name="FormsAuthentication" />
  <remove name="PassportAuthentication" />
  <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</httpModules>

最佳答案

由于您使用的是ASP.NET 2.0,因此需要安装ASP.NET AJAX扩展,如Joe Enos所述。我在这里有关于必要配置工作的更多信息:http://encosia.com/asmx-scriptservice-mistakes-installation-and-configuration/

此外,响应周围的.d包装器是an addition that didn't come until ASP.NET 3.5。因此,即使其他一切正常,您的msg.d在ASP.NET 2.0中也将是undefined。省略.d并使其:

success: function(msg) {
  // Replace the div's content with the page method's return.
  $("#Result").text(msg);
}

关于asp.net - 从客户端jQuery背后的代码中调用Webmethod,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10917309/

10-11 23:11
查看更多