在 vb.net 2.0 中使用 jQuery ajax

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14278508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:52:09  来源:igfitidea点击:

Using jQuery ajax with vb.net 2.0

jqueryasp.netajaxvb.net

提问by TheFrancisOne

I try to do use Ajax (through jQuery) in asp.net website (using vb.net 2.0).

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

Here is my .aspx page (ajax.aspx):

这是我的 .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>

And here is code-behind file (ajax.aspx.vb):

这是代码隐藏文件 (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

When I click on my div, ajax call failed with:

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

System.ArgumentException: Unknown web method HelloWorld.

System.ArgumentException:未知的 Web 方法 HelloWorld。

[EDIT]

[编辑]

After refresh and restart browser a lot of times, web method is found!

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

I try now to add a new web method, but I get error "unknown web method" again.

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

I think it's a cache problem.

我认为这是一个缓存问题。

Do you know how to force disable this kind of cache ?

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

采纳答案by TheFrancisOne

Finally, paritosh answer was useful.

最后,paritosh 的回答很有用。

I had to install ASP.NET Ajax 1.0 on web server using ASP.NET 2.0.

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

Here is the link : http://www.microsoft.com/en-us/download/details.aspx?id=883

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

Then, web method is not found until i restart ISS website, I don't know why, but it's OK after restarting it.

然后,直到我重新启动ISS网站才找到web方法,我不知道为什么,但重新启动后就可以了。

Thanks for answers.

感谢您的回答。

回答by Ton

change

改变

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

to

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

This will make all the web methods on this page visible to the JavaScript client.

这将使 JavaScript 客户端可以看到此页面上的所有 Web 方法。