javascript jQuery 跨域请求在 IE 中仍然失败,但使用 jsonp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12865053/
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 cross domain request still failing in IE, but using jsonp
提问by Horst Walter
My Ajax cross domain request is failing in IE 9 with "Access denied". I have read through several posts regarding this topic, and AFAIK it should work.
我的 Ajax 跨域请求在 IE 9 中失败并显示“拒绝访问”。我已经阅读了关于这个主题的几篇文章,AFAIK 它应该可以工作。
- IE9 and jQuery 1.8.1
- Call is
async
,jsonp
andcrossdomain
,cache
isfalse
. These are the prerequisites I have found. - Works in latest Firefox and Chrome.
jQuery.support.cors
is true- Even the response header is set:
Access-Control-Allow-Origin:*
(SO) - The returned JSON code would also be correct, have used a checker (also see 3.)
- IE9 和 jQuery 1.8.1
- 通话
async
,jsonp
并且crossdomain
,cache
是false
。这些是我找到的先决条件。 - 适用于最新的 Firefox 和 Chrome。
jQuery.support.cors
是真的- 甚至响应标头被设定:
Access-Control-Allow-Origin:*
(SO) - 返回的 JSON 代码也是正确的,使用了检查器(另见 3。)
So why is this failing with Access denied? Any idea? Could it be because my code is called from within a "JavaScript" library, and not a <script></script>
tag on the page?
那么为什么这会因Access denied而失败呢?任何的想法?可能是因为我的代码是从“JavaScript”库中调用的,而不是<script></script>
页面上的标签?
What am I missing?
我错过了什么?
// The code is part of an object's method (prototype)
// code resides in a library "Mylib.js"
$.ajax({
type: 'GET',
url: url,
cache: false,
async: true,
crossdomain: true, // typo, crossDomain, see my answer below
datatype: "jsonp", // dataType
success: function (data, status) {
if (status == "success" && !Object.isNullOrUndefined(data)) { ... }
},
error: function (xhr, textStatus, errorThrown) {
// access denied
}
});
-- Edit -- based on Robotsushi's comments, some further research ---
-- 编辑 -- 基于 Robotsushi 的评论,一些进一步的研究 ---
- Indeed, cannot find
XDomainRequest
in the jQuery source code (1.8.1) - If I do not set cors (
jQuery.support.cors = true
) I'll end up with a "No Transport" exception. - Still wondering why others obviously succeed with IE9 cross domain requests, e.g. here: jQuery Cross-Domain Ajax JSONP Calls Failing Randomly For Unknown Reasons In Some IE Versions
The way jQuery handles this, seems to be around the code below, but this is not called in my particular case, no idea why?
// Bind script tag hack transport jQuery.ajaxTransport( "script", function(s) {
// This transport only deals with cross domain requests if ( s.crossDomain ) {
A similar situation here in year 2010: Jquery $.ajax fails in IE on cross domain callsHowever, this should have been solved by the later jQuery versions.
- 确实,
XDomainRequest
在jQuery源代码(1.8.1)中找不到 - 如果我不设置 cors (
jQuery.support.cors = true
) 我最终会出现“无传输”异常。 - 仍然想知道为什么其他人在 IE9 跨域请求方面明显成功,例如:jQuery Cross-Domain Ajax JSONP Calls Failing Randomly For Unknown Reasons In Some IE Versions
jQuery 处理此问题的方式似乎与下面的代码有关,但在我的特定情况下并未调用此方法,不知道为什么?
// 绑定脚本标签 hack transport jQuery.ajaxTransport( "script", function(s) {
// This transport only deals with cross domain requests if ( s.crossDomain ) {
2010 年的类似情况:Jquery $.ajax 在跨域调用时在 IE 中失败但是,这应该由更高的 jQuery 版本解决。
回答by Horst Walter
Ok, working now. Couple of mistakes on my side:
好的,现在工作。我这边有几个错误:
- It is
crossDomain: true, dataType: "jsonp"
, typo - missed capital letters. - JSONP requests are nottransparent. The data are not simply JSON notation, but have to be wrapped in a Js function call (on the server side): see http://en.wikipedia.org/wiki/JSONPBasically this means, if you cannot modify the sent data, JSONP is not the right option for you.
- 它是
crossDomain: true, dataType: "jsonp"
,错字 - 错过了大写字母。 - JSONP请求是不是透明的。数据不是简单的 JSON 表示法,而是必须包含在 Js 函数调用中(在服务器端):参见http://en.wikipedia.org/wiki/JSONP基本上这意味着,如果您无法修改发送的数据, JSONP 不是您的正确选择。
All things considered, it works. So my personal checklist would be:
考虑到所有因素,它有效。所以我的个人清单是:
- Use
json
if possible (e.g. with Chrome, FF, or most likely IE10). Make sure response header is set:Access-Control-Allow-Origin:*
- If using
jsonp
, check:async: true
,jsonp
andcrossdomain: true
,cache
isfalse
,jQuery.support.cors
istrue
These are the prerequisites I have found. - Also make sure the
jsonp
response is a function call (JSON wrapped in function), not"just ordinary" JSON data.
json
尽可能使用(例如使用 Chrome、FF 或最有可能的 IE10)。确保设置了响应头:Access-Control-Allow-Origin:*
- 如果使用
jsonp
,检查:async: true
,jsonp
和crossdomain: true
,cache
是false
,jQuery.support.cors
是true
这些是我发现的先决条件。 - 还要确保
jsonp
响应是函数调用(包含在函数中的 JSON),而不是“只是普通的”JSON 数据。
回答by steswinbank
I've had a similar issue trying to access some json that I'm storing on Google Cloud Storage and accessing using jQuery's ajaxing. This worked fine in Chrome and Firefox, but I was getting 'access denied' messages when testing in IE (9 and below).
我在尝试访问我存储在 Google Cloud Storage 上的一些 json 并使用 jQuery 的 ajaxing 访问时遇到了类似的问题。这在 Chrome 和 Firefox 中运行良好,但在 IE(9 及以下)中进行测试时,我收到“访问被拒绝”消息。
The way I got around it was to use jsonP, explicitly:
我绕过它的方法是使用 jsonP,明确地:
- re-write my json file to be a javascript file with a javascript variable to hold the json data, e.g.
- 将我的 json 文件重写为带有 javascript 变量的 javascript 文件来保存 json 数据,例如
(function (o) {
variableName = [json];
}(window.[nameSpace] = window.[nameSpace]|| {}));
(function (o) {
variableName = [json];
}(window.[nameSpace] = window.[nameSpace]|| {}));
include the url to the javascript file within the tag of the html file, e.g.
<script type="application/javascript" src="[url to javascript file]"></script>
- Consume the data via it's variableName
在 html 文件的标签中包含 javascript 文件的 url,例如
<script type="application/javascript" src="[url to javascript file]"></script>
- 通过它的 variableName 消费数据
Hope this helps :)
希望这可以帮助 :)
回答by BentOnCoding
IE requires you to use XDomainRequest
instead of XHR for cross site.
IE 要求您使用XDomainRequest
而不是 XHR 进行跨站点。
You can check out the easyXDM which is a js library that abstracts this process for you.
您可以查看 easyXDM,它是一个为您抽象此过程的 js 库。
Alternatively see this :
或者看这个: