“命名空间”尝试从 jQuery 调用 asmx 方法时未定义

发布于 2024-09-27 08:24:37 字数 1309 浏览 0 评论 0原文

我正在尝试使用 jQuery 访问 asmx Web 服务中的方法。当我尝试执行下面的 jquery 调用时,出现错误“MyNameSpace”未定义。

jquery 调用 webservice:

  MyNameSpace.MyWebService.MyMethod(parameter, function (e) { alert('Success') }, function (e) { alert('Failure') });

scriptmanager:

  <asp:ScriptManager id="ScriptManager1" runat="server" >
     <Services>
       <asp:ServiceReference path="MyWebService.asmx" />
     </Services>
  </asp:ScriptManager>

MyWebService.asmx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Security;
using System.Web.Script.Services;


namespace MyNameSpace{
    [ScriptService]
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    //[System.Web.Script.Services.ScriptService]
    public class MyWebService : System.Web.Services.WebService
    {


        [WebMethod]
        public void MyMethod(string parameter)
        {
            //do some cool stuff


        }
    }
}

I'm attempting to use jQuery to access a method in an asmx webservice. When I try to execute the below jquery call, I get an error 'MyNameSpace' is undefined.

jquery call to webservice:

  MyNameSpace.MyWebService.MyMethod(parameter, function (e) { alert('Success') }, function (e) { alert('Failure') });

scriptmanager:

  <asp:ScriptManager id="ScriptManager1" runat="server" >
     <Services>
       <asp:ServiceReference path="MyWebService.asmx" />
     </Services>
  </asp:ScriptManager>

MyWebService.asmx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Security;
using System.Web.Script.Services;


namespace MyNameSpace{
    [ScriptService]
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    //[System.Web.Script.Services.ScriptService]
    public class MyWebService : System.Web.Services.WebService
    {


        [WebMethod]
        public void MyMethod(string parameter)
        {
            //do some cool stuff


        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若水微香 2024-10-04 08:24:37

事实证明,这是我的 .asmx 文件的问题,而且我已将其移动到另一个文件夹中。要修复我的 .asmx 文件,我右键单击它并选择视图标记。类属性设置为错误的类,因此我的脚本管理器无法加载它。

This turned out to be a problem with my .asmx file and the fact that I had moved it into a different folder. To fix my .asmx file, i right click on it and selected view markup. The class property was set to the wrong class so my scriptmanager couldn't load it.

一百个冬季 2024-10-04 08:24:37

是的,我也犯了“我会整理一下”的错误...

如果您更改了程序集根命名空间,请确保编辑 asmx 文件本身的“继承”属性中的引用。这并不总是显而易见的,因为 VS 不显示该文件的内容。

顺便提一句。在脚本管理器上临时设置 InlineScript="True" 将导致应用程序在这些情况下抛出实际异常,这使得更容易找出问题所在。

(虽然OP已经回答了他们自己的问题,但我认为这个经验值得补充以供将来参考。)

Yup, I also fell foul of "I'll just tidy that up"...

If you have changed the assembly root namespace, ensure that you edit the reference in the 'inherits' property of the the asmx file itself. This isn't always obvious as VS doesn't show the contents of this file.

btw. temporarily Setting InlineScript="True" on the Script Manager will cause the application to throw an actual exception in these cases, which makes it a lot easier to figure out what's going wrong.

(Although the OP had already answered their own question, I thought this experience worth adding for future reference.)

少女净妖师 2024-10-04 08:24:37

我将添加一个与上面给出的答案不同的答案。这是我最近遇到的一个边缘案例。

就我而言,加载脚本时遇到了计时问题。这意味着,在 Google Chrome 上,我的脚本加载良好并且能够访问 WebService 方法。但是,在 IE11 上,我的脚本无法加载,因为它是在 ScriptManager 加载之前加载的,因此 IE11 无法看到命名空间来访问 WebService 方法。

关于如何解决此计时问题有 2 种可能的建议。两者都不是解决这个罕见问题的可靠灵丹妙药。

将 ScriptReference 添加到 ScriptManager

正如 MSDN 中所述:

注册自定义脚本

使用 ScriptManager 控件来管理您为参与部分页面更新的控件创建的资源。资源包括脚本、样式、隐藏字段和数​​组。 ScriptManager 控件的 Scripts 集合包含浏览器可用的每个脚本的 ScriptReference 对象。您可以以声明方式或以编程方式指定脚本。
ScriptManager 控件还公开了注册方法,您可以使用这些方法以编程方式管理客户端脚本和隐藏字段。当您注册支持部分页面更新的脚本或隐藏字段时,必须调用 ScriptManager 控件的注册方法。 (要注册部分页面更新不需要的脚本,可以使用 ClientScriptManager 类的方法。)

换句话说,您必须像这样添加脚本:

<asp:ScriptManager ID="scriptManager" runat="server">
    <Services>
        <asp:ServiceReference Path="BackgroundService.asmx" />
    </Services>
    <Scripts>
        <asp:ScriptReference Path="Scripts/jquery-3.1.1.js" />
        <asp:ScriptReference Path="Scripts/customScript.js" />
        <asp:ScriptReference Path="Scripts/test_script_for_lulz.js" />
    </Scripts>
</asp:ScriptManager>

这样 ScriptManager 才能在更新时引用您的脚本。正在加载。

在 ScriptManager 之后添加脚本

另一种方法是在 ScriptManager 之后在

元素内添加脚本,如下所示:

<asp:ScriptManager ID="scriptManager" runat="server">
    <Services>
        <asp:ServiceReference Path="BackgroundService.asmx" />
    </Services>
</asp:ScriptManager>

<%-- Your scripts come after ScriptManager --%>
<script type="text/javascript" src="Scripts/jquery-3.1.1.js"></script>
<script type="text/javascript" src="Scripts/test_script.js"></script>
<script type="text/javascript" src="Scripts/lulz_script_again.js"></script>

这样,您的脚本就可以在由于浏览器读取标记语言的方式,在 ScriptManager 完成加载后加载。

同样,不能保证哪种解决方案比另一种更好。这是我遇到过的时间问题,仅此而已。

I'm going to add a different answer than what was given above. This is an edge case that I had encountered just recently.

In my case, I have been hit with timing issues when my scripts are being loaded. This means, on Google Chrome, my script loads fine and is able to access the WebService methods. However, on IE11, my script fails to load, because it is loaded before ScriptManager is loaded in, and thus, IE11 can't see the namespace in order to access the WebService methods.

There are 2 possible suggestions on how to fix this timing issue. Both are not reliably the silver bullets for this rare issue.

Adding ScriptReference to the ScriptManager

As mentioned on MSDN:

Registering Custom Script

Use the ScriptManager control to manage resources that you have created for controls that participate in partial-page updates. Resources include scripts, styles, hidden fields, and arrays. The Scripts collection of the ScriptManager control contains a ScriptReference object for each script that is available to the browser. You can specify the scripts declaratively or programmatically.
The ScriptManager control also exposes registration methods that you can use to manage client script and hidden fields programmatically. When you are registering script or hidden fields that support partial-page updates, you must call registration methods of the ScriptManager control. (To register scripts that are not needed for partial-page updates, you use methods of the ClientScriptManager class.)

In other words, you have to add your scripts like this:

<asp:ScriptManager ID="scriptManager" runat="server">
    <Services>
        <asp:ServiceReference Path="BackgroundService.asmx" />
    </Services>
    <Scripts>
        <asp:ScriptReference Path="Scripts/jquery-3.1.1.js" />
        <asp:ScriptReference Path="Scripts/customScript.js" />
        <asp:ScriptReference Path="Scripts/test_script_for_lulz.js" />
    </Scripts>
</asp:ScriptManager>

This is so the ScriptManager is able to reference your scripts when it is being loaded.

Adding Your Scripts After ScriptManager

The other way of doing this is to add your scripts after ScriptManager, inside your <form runat="server"> element, like this:

<asp:ScriptManager ID="scriptManager" runat="server">
    <Services>
        <asp:ServiceReference Path="BackgroundService.asmx" />
    </Services>
</asp:ScriptManager>

<%-- Your scripts come after ScriptManager --%>
<script type="text/javascript" src="Scripts/jquery-3.1.1.js"></script>
<script type="text/javascript" src="Scripts/test_script.js"></script>
<script type="text/javascript" src="Scripts/lulz_script_again.js"></script>

This way, your scripts are then loaded after ScriptManager finishes loading, due to the way the markup language is read by the browser.

Again, no guarantees on which solution is the better than the other. It's a timing issue that I've encountered, that's all.

怪我太投入 2024-10-04 08:24:37

另一件需要注意的事情是确保您传递的参数具有正确的数据类型。同样的问题也发生在我身上,我忘记将数组转换为服务器端方法接受的字符串。

One more thing to note is that make sure that you are passing the parameters with the correct data type. The same issue happens to me where I forgot to convert the array into a string that the server-side method accepts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文