0

我正在尝试修改一些在 IE8 中中断的现有代码。根据我在网上阅读的内容,这是涉及 XDomainRequest 和 XMLHttpRequest 的常见问题。

我以前没有以这种方式使用过 AJAX,所以我完全不知道该怎么做。代码如下:(我已将括号“[[”和“]]”中的敏感信息替换为任意变量名称,以识别每个变量的使用位置):

jQuery(document).ready(function() {

    var base_url = '[[BASE_URL_1]]';   // DTRT for ajax requests (CORS over HTTP by default)
    if (document.location.protocol == "https:") {
        base_url = '[[BASE_URL_2]]';    // The proxy script that lets us do secure cross-domain AJAX requests
    }
    var classSpec = '$class_spec';

    $('#[[ID_1]]').empty().addClass('loading'); // loading gif
    $('#[[ID_2]]').empty().addClass('loading');

    $.ajax( {
         type: "GET",
          url: base_url + '[[URL_1]]' + classSpec + '[[URL_2]]',
      success: function (data) { $('[[ID_2]]').html(data).removeClass('loading'); }
    } );

    $.ajax( {
         type: "GET",
          url: base_url + '[[URL_1]' + classSpec + '[[URL_3]]',
      success: function (data) { $('[[ID+1]]').html(data).removeClass('loading'); initialize(); }
    } );

});

如何修改此代码以包含对 IE8 的支持?

4

2 回答 2

1

To do cross domain requests you will probably need to check the following

  1. jQuery.support.cors = true;
  2. crossDomain = true;
  3. Access-Control-Allow-Origin headers

1 & 2

jQuery(document).ready(function() {

    var base_url = '[[BASE_URL_1]]';   // DTRT for ajax requests (CORS over HTTP by default)
    if (document.location.protocol == "https:") {
        base_url = '[[BASE_URL_2]]';    // The proxy script that lets us do secure cross-domain AJAX requests
    }
    var classSpec = '$class_spec';

    $('#[[ID_1]]').empty().addClass('loading'); // loading gif
    $('#[[ID_2]]').empty().addClass('loading');

    jQuery.support.cors = true;
    $.ajax(
    {
        crossDomain: true,
        type: "GET",
        url: base_url + '[[URL_1]]' + classSpec + '[[URL_2]]',
        success: function (data) { $('[[ID_2]]').html(data).removeClass('loading'); }
    } );

    $.ajax(
    {
        crossDomain: true,
        type: "GET",
        url: base_url + '[[URL_1]' + classSpec + '[[URL_3]]',
        success: function (data) { $('[[ID+1]]').html(data).removeClass('loading'); initialize(); }
    } );
});

3) Actually requires you to have access to the server you want to talk to. Basically the distant server says to the browser: Hey i allow these domains to do XDR requests, if you are not one of them ..please don't send any. (personally this is the craziest security scenario i have ever seen ..since it's up to the requester, to refrain itself from making requests to domains that don't want requests from him. That really sounds secure, hein !).

So if you are on server site1.com and want to XDR to site2.com, then site2.com must return a header like

Access-Control-Allow-Origin: *

or

Access-Control-Allow-Origin: site1.com

When working with IIS servers, you will probably also need to supply: Access-Control-Allow-Methods


The above normally applies to ALL browsers, not just IE8, for IE8 specifics have a look here: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx

于 2013-10-19T19:49:46.477 回答
0

我建议尝试这个库: https ://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest

在 jQuery 1.5+ 中使用 $.ajax 函数时,使用 IE8 和 IE9 的 XDomainRequest 对象实现自动跨域资源共享支持"

它确实具有与 XDomainRequest 相同的限制,即:

为了在 Internet Explorer 中使用 XDomainRequest,请求必须是:

  • 仅 GET 或 POST
    • POST 时,数据将始终以 text/plain 的 Content-Type 发送
  • 仅 HTTP 或 HTTPS
    • 协议必须与调用页面的方案相同
  • 始终异步

看起来你正在检查匹配的协议,所以你应该在那里被覆盖。

于 2014-09-03T18:08:43.833 回答