【问题标题】:Using jQuery ajax with vb.net 2.0在 vb.net 2.0 中使用 jQuery ajax
【发布时间】:2012-12-26 01:19:59
【问题描述】:

我尝试在 asp.net 网站(使用 vb.net 2.0)中使用 Ajax(通过 jQuery)。

这是我的 .aspx 页面 (ajax.aspx):

    <%@ Page Language="vb" AutoEventWireup="true" CodeFile="ajax.aspx.vb" Inherits="_Ajax" %>

<!DOCTYPE html>
<html>
<head>
  <title>Calling page methods with jQuery</title>
  <style type="text/css">
    #Result {
      cursor: pointer;
    }
  </style>
</head>
<body>
  <div id="Result">Click here for the time.</div>

  <script src="jquery-1.8.3.js"></script>
  <script>
      $('#Result').click(function () {
          $.ajax({
              type: "POST",
              url: "ajax.aspx/HelloWorld",
              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);
              },
              error: function (xhr, ajaxOptions, thrownError) {
                  alert("xhr.responseText= " + xhr.responseText);
                  alert("xhr.responseStatus= " + xhr.responseStatus);
                  alert("thrownError= " + thrownError);
              }
          });
      });
  </script>
</body>
</html>

这里是代码隐藏文件(ajax.aspx.vb):

    Imports System
Imports System.Web.Services

Public Class _Ajax
    Inherits System.Web.UI.Page

    Protected Sub Page_Load()

    End Sub

    <WebMethod()> _
    Public Shared Function HelloWorld() As String
        Return "Hello: "
    End Function

End Class

当我点击我的 div 时,ajax 调用失败:

System.ArgumentException:未知的网络方法 HelloWorld。

[编辑]

多次刷新重启浏览器后,发现web方法!

我现在尝试添加新的网络方法,但我再次收到错误“未知网络方法”。

我认为是缓存问题。

你知道如何强制禁用这种缓存吗?

【问题讨论】:

  • 您可能缺少一些 web.config 值(httpmodules),您可以看到您可以在页面中访问 scriptmanager。如果没有,您需要为您的应用程序安装 ajax 扩展。
  • 你说得对,安装 Ajax for ASP.NET 2.0 后更好(但还不够)
  • 你的问题解决了吗?或者你还有一些问题
  • 没有解决。我收到错误“未知的网络方法”。我编辑了我的第一篇文章来描述它。

标签: jquery asp.net ajax vb.net


【解决方案1】:

最后,paritosh 的回答很有用。

我必须使用 ASP.NET 2.0 在 Web 服务器上安装 ASP.NET Ajax 1.0。

这里是链接: http://www.microsoft.com/en-us/download/details.aspx?id=883

然后,就是重启ISS网站才发现web方法,不知道是什么原因,重启后就OK了。

感谢您的回答。

【讨论】:

    【解决方案2】:

    改变

    <%@ Page Language="vb" AutoEventWireup="true" CodeFile="ajax.aspx.vb"
     Inherits="_Ajax" %>
    

    到

     <%@ Page Language="vb" AutoEventWireup="true" CodeFile="ajax.aspx.vb"
     Inherits="_Ajax" ScriptManager.EnablePageMethods="true" %>
    

    这将使该页面上的所有 Web 方法对 JavaScript 客户端可见。

    【讨论】:

    • EnablePageMethods 不适用于 ASP.NET 2.0。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 2010-10-27
    • 2011-03-09
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 2012-01-29
    相关资源
    最近更新 更多