“命名空间”尝试从 jQuery 调用 asmx 方法时未定义
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
事实证明,这是我的 .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.
是的,我也犯了“我会整理一下”的错误...
如果您更改了程序集根命名空间,请确保编辑 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.)
我将添加一个与上面给出的答案不同的答案。这是我最近遇到的一个边缘案例。
就我而言,加载脚本时遇到了计时问题。这意味着,在 Google Chrome 上,我的脚本加载良好并且能够访问 WebService 方法。但是,在 IE11 上,我的脚本无法加载,因为它是在 ScriptManager 加载之前加载的,因此 IE11 无法看到命名空间来访问 WebService 方法。
关于如何解决此计时问题有 2 种可能的建议。两者都不是解决这个罕见问题的可靠灵丹妙药。
将 ScriptReference 添加到 ScriptManager
正如 MSDN 中所述:
换句话说,您必须像这样添加脚本:
这样 ScriptManager 才能在更新时引用您的脚本。正在加载。
在 ScriptManager 之后添加脚本
另一种方法是在 ScriptManager 之后在
这样,您的脚本就可以在由于浏览器读取标记语言的方式,在 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:
In other words, you have to add your scripts like this:
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: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.
另一件需要注意的事情是确保您传递的参数具有正确的数据类型。同样的问题也发生在我身上,我忘记将数组转换为服务器端方法接受的字符串。
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.