1

我使用 JQuery ajax 跨域获取 Json 数据。我的代码可以在 Firefox 和 Chrome 下运行,但 IE 是个大麻烦。它在 IE 10 中工作,但不少于。我需要让它在更少的 IE8 或更高版本上运行,因为它们仍然是大多数。这是我的代码:

jQuery.support.cors = true;   
$.ajax({
    type: 'GET',
    url: "https:xxx.com",
    crossDomain: true,

    cache: false,
    dataType: "json",

    headers : {
        "Accept-Datetime" :  "Tue, 12 Feb 2013 00:00:00 GMT",
        "Authorization" : "Basic XXXXXXXX"
    },

    success: processDetailsViewData,
   error: function(xhr, status, error){ 
   console.log(xhr.responseText, status, error);
   }
}); 

我尝试使用 jQuery.XDomainRequest.js,它只能在 http 或 https 之间工作。我不需要任何回调,我需要在请求中包含“Accept-Datetime”和“Authorization”。所以我想我不需要使用 JsonP。以前有人处理过这个案子吗?干杯。

4

1 回答 1

1
  1. 确保您的 URL 允许 ajax 请求。

  2. 确保目标上有一个 crossdomain.xml

  3. 某些跨域请求在 IE 中无法使用 jquery 查找,因此您必须覆盖 Jquery 库中的 ajax 函数。

--

使用 PHP 在目标中允许 ajax:

header('Access-Control-Allow-Origin: *');

--

您可以下载 html5boilerplate 以查找 crossdomain.xml 示例

--

要覆盖 jquery 方法,你可以试试这个

(function( jQuery ) {

if ( window.XDomainRequest && !jQuery.support.cors ) {
    jQuery.ajaxTransport(function( s ) {
        if ( s.crossDomain && s.async ) {
            if ( s.timeout ) {
                s.xdrTimeout = s.timeout;
                delete s.timeout;
            }
            var xdr;
            return {
                send: function( _, complete ) {
                    function callback( status, statusText, responses, responseHeaders ) {
                        xdr.onload = xdr.onerror = xdr.ontimeout = xdr.onprogress = jQuery.noop;
                        xdr = undefined;
                        jQuery.event.trigger( "ajaxStop" );
                        complete( status, statusText, responses, responseHeaders );
                    }
                    xdr = new XDomainRequest();
                    xdr.open( s.type, s.url );
                    xdr.onload = function() {
                        var status = 200;
                        var message = xdr.responseText;
                        var r = jQuery.parseJSON(xdr.responseText);
                        if (r.StatusCode && r.Message) {
                            status = r.StatusCode;
                            message = r.Message;
                        }
                        callback( status , message, { text: message }, "Content-Type: " + xdr.contentType );
                    };
                    xdr.onerror = function() {
                        callback( 500, "Unable to Process Data" );
                    };
                    xdr.onprogress = function() {};
                    if ( s.xdrTimeout ) {
                        xdr.ontimeout = function() {
                            callback( 0, "timeout" );
                        };
                        xdr.timeout = s.xdrTimeout;
                    }
                    xdr.send( ( s.hasContent && s.data ) || null );
                },
                abort: function() {
                    if ( xdr ) {
                        xdr.onerror = jQuery.noop();
                        xdr.abort();
                    }
                }
            };
        }
    });
}
})( jQuery );
于 2013-07-05T09:35:07.533 回答