【问题标题】:JS CORS issue with IE - how to resolve?IE 的 JS CORS 问题 - 如何解决?
【发布时间】:2012-05-24 14:42:18
【问题描述】:

我知道这已经被很多人访问过,并且我已经设法让它与其他项目一起工作。我继承了这块在 IE 中不起作用的代码:

function loadTemplate(template, alt_cache_name) {
    if (templates[template] !== undefined) {
        if (alt_cache_name !== undefined) {
            templates[alt_cache_name] = templates[template];
        }
        return templates[template];
    } else {

        return $.get(template, function (response) {
            if (alt_cache_name !== undefined) {
                templates[alt_cache_name] = response;
            }
            templates[template] = response;
        }, 'text');
    }
}

问题在于 $.get()。

template 的值是指向特定 html 模板的 url。

我尝试了以下,但我认为返回类型与 $.get() 返回的类型不同:

function createCORSRequest(method, url) {
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr) {
        xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined") {
        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {
        xhr = null;
    }
    return xhr;
}


function loadTemplate(template, alt_cache_name) {
    if (templates[template] !== undefined) {
        if (alt_cache_name !== undefined) {
            templates[alt_cache_name] = templates[template];
        }
        return templates[template];
    } else {

        var request = createCORSRequest("GET", template);
        var content = "";
        if (request) {
            request.onload = function () {
                //do something with request.responseText
                console.log(request.responseText);
                if (alt_cache_name !== undefined) {
                    templates[alt_cache_name] = response.responseText;
                }
                templates[template] = response.responseText;
            };
        }

        return request;


        //            return $.get(template, function (response) {
        //                if (alt_cache_name !== undefined) {
        //                    templates[alt_cache_name] = response;
        //                }
        //                templates[template] = response;
        //            }, 'text');
    }
}

我什至尝试了$.get

jQuery.support.cors = true;

有人能解释一下如何让它工作吗?

【问题讨论】:

    标签: jquery cross-domain getjson cors


    【解决方案1】:

    事实证明,here 列出的 jQuery 插件成功了。进一步阅读,this post 也详细介绍了解决方案。

    -- 更新--

    经过进一步测试,IE9 和这个库似乎存在一些问题。还有其他人遇到任何问题吗?

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题,我无法在运行时使用 IE 8/9 上的 CORS 从 CDN 加载模板。我可以通过在此处包含 xhr-xdr-adapter.js 文件来使其正常工作:https://github.com/intuit/xhr-xdr-adapter/blob/master/src/xhr-xdr-adapter.js

      您可以在需要的地方包含它,如下所示:

      <!--[if lt IE 10]>
      <script src="xhr-xdr-adapter.js" type="text/javascript"></script>
      <![endif]-->
      

      xhr-xdr-adapter 将 XMLHttpRequest 替换为不同的函数,该函数在运行时确定是使用本机 XMLHttpRequest 还是 XDomainRequest。通过包含它,我已经能够使用 jQuery 和 AngularJS 跨域加载模板。

      【讨论】:

        猜你喜欢
        • 2018-08-06
        • 2019-12-07
        • 2021-02-17
        • 1970-01-01
        • 1970-01-01
        • 2020-11-15
        • 1970-01-01
        • 2019-12-29
        • 2021-11-14
        相关资源
        最近更新 更多