jQuery 调用 WebService 返回“无传输”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5241088/
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
jQuery Call to WebService returns "No Transport" error
提问by griegs
I have the following web service;
我有以下网络服务;
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
It's stock standard with no alterations to the class decorators.
它是标准的,没有改变类装饰器。
I have this jQuery method;
我有这个 jQuery 方法;
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.
这是一个发布操作,因为稍后我需要将数据发布到它。
When I execute the jQuery I get a "No transport" error returned.
当我执行 jQuery 时,我得到一个“无传输”错误返回。
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.
我还应该提到的一件事是,jQuery 存储在我机器上的一个简单 HTML 文件中,并且 WebService 也在我的机器上运行。
There is no code behind on the HTML page it's simply a web page and not a c# project or anything.
HTML 页面上没有任何代码,它只是一个网页,而不是 ac# 项目或任何东西。
Can anyone please point me in the right direction here?
任何人都可以在这里指出我正确的方向吗?
采纳答案by no.good.at.coding
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.
如果您的 jQuery 页面没有被加载,http://localhost:54473
那么这个问题可能是因为您正在尝试进行跨域请求。
Update 1Take a look at this blog post.
更新 1看看这篇博文。
Update 2If 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:
更新 2如果这确实是问题(我怀疑是),您可能需要查看 JSONP 作为解决方案。以下是一些可以帮助您入门的链接:
回答by SrBlanco
Add this: jQuery.support.cors = true;
添加这个: jQuery.support.cors = true;
It enables cross-site scripting in jQuery (introduced after 1.4x, I believe).
它支持 jQuery 中的跨站点脚本(我相信是在 1.4x 之后引入的)。
We were using a really old version of jQuery (1.3.2) and swapped it out for 1.6.1. Everything was working, except .ajax() calls. Adding the above line fixed the problem.
我们使用了一个非常旧的 jQuery (1.3.2) 版本,并将其换成了 1.6.1。一切正常,除了 .ajax() 调用。添加上面的行解决了问题。
回答by bsuttor
I had the same error on a page, and I added these lines:
我在页面上遇到了同样的错误,我添加了以下几行:
<!--[if lte IE 9]>
<script type='text/javascript' src='//cdnjs.cloudflare.com/ajax/libs/jquery-ajaxtransport-xdomainrequest/1.0.3/jquery.xdomainrequest.min.js'></script>
<![endif]-->
and it finally works for me ;) no more error in IE9.
它终于对我有用了 ;) 在 IE9 中不再有错误。
回答by rynop
None of the proposed answers completely worked for me. My use case is slightly different (doing an ajax get to an S3 .json file in IE9). Setting jQuery.support.cors = true;
got rid of the No Transport
error but I was still getting Permission denied
errors.
没有一个建议的答案完全适合我。我的用例略有不同(在 IE9 中对 S3 .json 文件执行 ajax 获取)。设置jQuery.support.cors = true;
摆脱了No Transport
错误,但我仍然遇到Permission denied
错误。
What did work for me was to use the jQuery-ajaxTransport-XDomainRequestto force IE9 to use XDomainRequest. Using this did not require setting jQuery.support.cors = true;
对我有用的是使用jQuery-ajaxTransport-XDomainRequest强制 IE9 使用 XDomainRequest。使用这个不需要设置jQuery.support.cors = true;
回答by Abhishek
i solve it by using dataType='jsonp' at the place of dataType='json'
我通过在 dataType='json' 的地方使用 dataType='jsonp' 来解决它
回答by Riju Mahna
I too got this problem and all solutions given above either failed or were not applicable due to client webservice restrictions.
我也遇到了这个问题,由于客户端网络服务限制,上面给出的所有解决方案要么失败,要么不适用。
For this, I added an iframe in my page which resided in the client;s server. So when we post our data to the iframe and the iframe then posts it to the webservice. Hence the cross-domain referencing is eliminated.
为此,我在位于客户端服务器的页面中添加了一个 iframe。因此,当我们将数据发布到 iframe,然后 iframe 将其发布到 Web 服务。因此消除了跨域引用。
We added a 2-way origin check to confirm only authorized page posts data to and from the iframe.
我们添加了一个 2 向来源检查,以确认只有经过授权的页面才能向 iframe 和从 iframe 发布数据。
Hope it helps
希望能帮助到你
<iframe style="display:none;" id='receiver' name="receiver" src="https://iframe-address-at-client-server">
</iframe>
//send data to iframe
var hiddenFrame = document.getElementById('receiver').contentWindow;
hiddenFrame.postMessage(JSON.stringify(message), 'https://client-server-url');
//The iframe receives the data using the code:
window.onload = function () {
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
eventer(messageEvent, function (e) {
var origin = e.origin;
//if origin not in pre-defined list, break and return
var messageFromParent = JSON.parse(e.data);
var json = messageFromParent.data;
//send json to web service using AJAX
//return the response back to source
e.source.postMessage(JSON.stringify(aJAXResponse), e.origin);
}, false);
}
回答by Curious Sam
For me it is an entirely different story.
Since this page has a good search engine ranking, I should add my case and the solution here too.
对我来说,这是一个完全不同的故事。
由于此页面具有良好的搜索引擎排名,因此我也应该在此处添加我的案例和解决方案。
I built jquery
myself with webpack
picking only the modules I use. The ajax is always failed with "No Transport" message as the only clue.
我只选择我使用的模块来构建jquery
自己webpack
。ajax 总是失败,“无传输”消息是唯一的线索。
After a long debugging, the problem turns out to be XMLHttpRequest
is pluggable in jquery
and it not include by default.
经过长时间的调试,问题原来XMLHttpRequest
是可插入的,jquery
默认情况下不包含。
You have to explicitly include jquery/src/ajax/xhr
file in order to make the ajax working in browsers.
您必须明确包含jquery/src/ajax/xhr
文件才能使 ajax 在浏览器中工作。
回答by Draghon
I solved it simply by removing the domain from the request url.
我只是通过从请求 url 中删除域来解决它。
Before: https://some.domain.com/_vti_bin/service.svc
After: /_vti_bin/service.svc