jQuery跨域请求仍然失败在IE,但使用jsonp [英] jQuery cross domain request still failing in IE, but using jsonp

查看:892
本文介绍了jQuery跨域请求仍然失败在IE,但使用jsonp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Ajax跨网域要求在IE 9的「存取被拒绝」中失败。我已阅读过有关这个主题的几个帖子,AFAIK应该工作。

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.


  1. IE9和jQuery 1.8.1

  2. 调用 async jsonp crossdomain cache false 。这些是我找到的先决条件。

  3. 适用于最新的Firefox和Chrome。

  4. jQuery.support.cors / li>
  5. 即使设置了响应头: Access-Control-Allow-Origin:* SO

  6. 返回的JSON代码也正确, (另见3。)

  1. IE9 and jQuery 1.8.1
  2. Call is async, jsonp and crossdomain, cache is false. These are the prerequisites I have found.
  3. Works in latest Firefox and Chrome.
  4. jQuery.support.cors is true
  5. Even the response header is set: Access-Control-Allow-Origin:* (SO)
  6. The returned JSON code would also be correct, have used a checker (also see 3.)

那么为什么这种情况会出现访问被拒绝任何想法?是因为我的代码是从JavaScript库中调用的,而不是在页面上的< script>< / script> 标记?

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?

我缺少什么?

    // 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
        }
    });

- 编辑 - 基于Robotsushi的意见,一些进一步的研究---

-- Edit -- based on Robotsushi's comments, some further research ---

jQuery处理这个问题的方式似乎在下面的代码中,但是在我的具体情况下不调用,不知道为什么?

  1. Indeed, cannot find XDomainRequest in the jQuery source code (1.8.1)
  2. If I do not set cors (jQuery.support.cors = true) I'll end up with a "No Transport" exception.
  3. 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
  4. The way jQuery handles this, seems to be around the code below, but this is not called in my particular case, no idea why?

//绑定脚本标签hack transport
jQuery.ajaxTransport(script {

// Bind script tag hack transport jQuery.ajaxTransport( "script", function(s) {

// This transport only deals with cross domain requests
if ( s.crossDomain ) {


  • 2010年的情况类似: Jquery $ .ajax在跨域调用的IE中失败但是,这应该是由后来的jQuery版本。

  • A similar situation here in year 2010: Jquery $.ajax fails in IE on cross domain calls However, this should have been solved by the later jQuery versions.


    推荐答案

    我的身边:


    1. 它是 crossDomain:true,dataType:jsonp

    2. JSONP请求是透明的。数据不仅仅是JSON记号,而是必须包含在Js函数调用中(在服务器端):请参见 http:// en.wikipedia.org/wiki/JSONP 基本上这意味着,如果你不能修改发送的数据,JSONP不是你的正确选择。

    1. It is crossDomain: true, dataType: "jsonp" , typo - missed capital letters.
    2. JSONP requests are not transparent. 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/JSONP Basically this means, if you cannot modify the sent data, JSONP is not the right option for you.

    所有的事情,它的工作原理。所以我的个人清单是:

    All things considered, it works. So my personal checklist would be:


    1. 如果可能的话,使用 json ,FF或最可能IE10)。确保响应标头已设置: Access-Control-Allow-Origin:*

    2. 如果使用 jsonp ,检查: async:true jsonp crossdomain:true , false / code>是 true 这是我找到的先决条件。

    3. 还要确保 jsonp 响应是一个函数调用( JSON包装在函数),只是普通的JSON数据。

    1. Use json if possible (e.g. with Chrome, FF, or most likely IE10). Make sure response header is set: Access-Control-Allow-Origin:*
    2. If using jsonp, check: async: true, jsonp and crossdomain: true, cache is false, jQuery.support.cors is true These are the prerequisites I have found.
    3. Also make sure the jsonp response is a function call (JSON wrapped in function), not "just ordinary" JSON data.

    这篇关于jQuery跨域请求仍然失败在IE,但使用jsonp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆