对 WebService 的 jQuery 调用返回“无传输";错误 [英] jQuery Call to WebService returns "No Transport" error
问题描述
我有以下网络服务;
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
它是标准的,没有改变类装饰器.
It's stock standard with no alterations to the class decorators.
我有这个 jQuery 方法;
I have this jQuery method;
var webMethod = "http://localhost:54473/Service1.asmx/HelloWorld";
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
url: webMethod,
success: function(msg){ alert(msg.d); },
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
这是一个发布操作,因为稍后我需要将数据发布到它.
It's a post action because later on I need to post data to it.
当我执行 jQuery 时,我得到一个无传输"错误返回.
When I execute the jQuery I get a "No transport" error returned.
我还应该提到的一件事是,jQuery 存储在我机器上的一个简单 HTML 文件中,并且 WebService 也在我的机器上运行.
One thing I should also mention is that the jQuery is stored in a simple HTML file on my machine and the WebService is running on my machine also.
HTML 页面上没有隐藏代码,它只是一个网页,而不是 C# 项目或任何东西.
There is no code behind on the HTML page it's simply a web page and not a c# project or anything.
有人能在这里指出我正确的方向吗?
Can anyone please point me in the right direction here?
推荐答案
如果你的 jQuery 页面没有从 http://localhost:54473
加载,那么这个问题可能是因为你尝试进行跨域请求.
If your jQuery page isn't being loaded from http://localhost:54473
then this issue is probably because you're trying to make cross-domain request.
更新 1看看这篇博文.
更新 2如果这确实是问题(我怀疑是),您可能需要查看 JSONP 作为解决方案.以下是一些可以帮助您入门的链接:
Update 2 If this is indeed the problem (and I suspect it is), you might want to check out JSONP as a solution. Here are a few links that might help you get started:
- https://en.wikipedia.org/wiki/JSON
- http://remysharp.com/2007/10/08/what-is-jsonp/
- http://www.west-wind.com/weblog/posts/107136.aspx一个>
这篇关于对 WebService 的 jQuery 调用返回“无传输";错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!