jQuery IE 中带有 jQ​​uery ajax 调用的“无传输”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9160123/
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-08-27 10:26:26  来源:igfitidea点击:

'No Transport' Error w/ jQuery ajax call in IE

ajaxinternet-explorerjquerycross-domain

提问by Sang

I need to use foursquare API to search venues. Of course it is cross-domain.

我需要使用foursquare API 来搜索场地。当然是跨域的。

It has no any problems in Firefox but in Internet Explorer (7, 8, 9 I've tested).

它在 Firefox 中没有任何问题,但在 Internet Explorer 中(我已经测试过 7、8、9)。

My javascript code looks like:

我的 javascript 代码如下所示:

searchVenues: function(searchQuery) {
    $.ajax({
       url: 'https://api.foursquare.com/v2/venues/search',
       data: {
            sw: bound_south_west,
            ne: bound_north_east,
            query: searchQuery.query,
            oauth_token: FSQ_OAUTH_TOKEN,
            limit: 25,
            intent: 'browse',
            v: 20120206
       },
       cache: false,
       dataType: 'json',
       success: function(data) {
           displayResults(data, searchQuery.query);
       },
       error: function(xhr, status, errorThrown) {
           console.log(errorThrown+'\n'+status+'\n'+xhr.statusText);
       }
    });
}

In Firefox, it perfectly displays received data. In Internet Explorer, it logs on console:

在 Firefox 中,它完美地显示接收到的数据。在 Internet Explorer 中,它登录到控制台:

No Transport
Error
Error

What should I do?

我该怎么办?

回答by Magico

I tested this on Windows Mobile 7.

我在 Windows Mobile 7 上对此进行了测试。

After LOTS of time spent to understand, I finally found this:

在花了很多时间理解之后,我终于找到了这个:

http://bugs.jquery.com/ticket/10660

http://bugs.jquery.com/ticket/10660

The Solution is simple, just set this:

解决方案很简单,只需设置:

$.support.cors = true;

and Ajax cross domain requests will work!

并且 Ajax 跨域请求将起作用!

回答by amit thakur

jQuery.support.cors = true;

$.ajax({
  crossDomain: true,
  url: "",
  type: "POST",
  dataType: "xml",
  data: soapMessage,
});

you need to make the cross domain value to true

您需要将跨域值设为 true

回答by Alex D

This problem has been bugging me for some time. As a workaround I use proxy scripts located on the same site. Such scripts simply execute server-to-server non-ajax HTTP request (think of curl and WinHttp.WinHttpRequest) and pass status and data back to the caller. It works, but obviously not very efficient because it has to perform two HTTP requests.

这个问题已经困扰我一段时间了。作为一种解决方法,我使用位于同一站点上的代理脚本。此类脚本只是执行服务器到服务器的非 ajax HTTP 请求(想想 curl 和 WinHttp.WinHttpRequest)并将状态和数据传递回调用者。它可以工作,但显然效率不高,因为它必须执行两个 HTTP 请求。

In my case, solution is a combination of all the things described above plus 'Access-Control-Allow-Origin' header.

就我而言,解决方案是上述所有内容加上“Access-Control-Allow-Origin”标头的组合。

$.support.cors = true; // this must precede $.ajax({}) configuration

$.ajax({
  crossDomain: true, // added in jQuery 1.5
  headers: {
    'Access-Control-Allow-Origin': '*'
  },
  ...
});

The web service that answers these calls also responds with 'Access-Control-Allow-Origin: *' header.

应答这些调用的 Web 服务也会响应“Access-Control-Allow-Origin: *”标头。

回答by Scott Stafford

Try this solution:

试试这个解决方案:

https://stackoverflow.com/a/14463975/237091

https://stackoverflow.com/a/14463975/237091

Or, simply put this code in your HTML right after including jquery.

或者,只需在包含 jquery 后立即将此代码放入 HTML 中。

<!--[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]-->