0

跨域请求如何在 IE8 中工作?目标请求是

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false

我尝试使用 $.getJson 和 $.ajax,它们适用于 IE9/10 和 FF 和 chrome。但是它不适用于 IE8。我阅读了一些与让 XHR 工作相关的帖子,在 ajax 设置中启用 cors 标志后,我能够拨打电话。但是,我的回调从未被调用。

后来我通过使用 Google 的地理编码器 API 让它工作了。但是,我想知道如何在没有 API 的情况下让它工作

谢谢-维努

4

1 回答 1

0

我也遇到了这个问题(不确定目标浏览器),但是使用下面的代码,我得到了解决。在发出跨域请求之前使用这个代码

if (!jQuery.support.cors && window.XDomainRequest) {
    var httpRegEx = /^https?:\/\//i;
    var getOrPostRegEx = /^get|post$/i;
    var sameSchemeRegEx = new RegExp('^' + location.protocol, 'i');
    var xmlRegEx = /\/xml/i;

    // ajaxTransport exists in jQuery 1.5+
    jQuery.ajaxTransport('text html xml json', function (options, userOptions, jqXHR) {
        // XDomainRequests must be: asynchronous, GET or POST methods, HTTP or HTTPS protocol, and same scheme as calling page
        if (options.crossDomain && options.async && getOrPostRegEx.test(options.type) && httpRegEx.test(userOptions.url) && sameSchemeRegEx.test(userOptions.url)) {
            var xdr = null;
            var userType = (userOptions.dataType || '').toLowerCase();
            return {
                send: function (headers, complete) {
                    xdr = new XDomainRequest();
                    if (/^\d+$/.test(userOptions.timeout)) {
                        xdr.timeout = userOptions.timeout;
                    }
                    xdr.ontimeout = function () {
                        complete(500, 'timeout');
                    };
                    xdr.onload = function () {
                        var allResponseHeaders = 'Content-Length: ' + xdr.responseText.length + '\r\nContent-Type: ' + xdr.contentType;
                        var status = {
                            code: 200,
                            message: 'success'
                        };
                        var responses = {
                            text: xdr.responseText
                        };

                        try {
                            if (userType === 'json') {
                                try {
                                    responses.json = JSON.parse(xdr.responseText);
                                } catch (e) {
                                    status.code = 500;
                                    status.message = 'parseerror';
                                    //throw 'Invalid JSON: ' + xdr.responseText;
                                }
                            } else if ((userType === 'xml') || ((userType !== 'text') && xmlRegEx.test(xdr.contentType))) {
                                var doc = new ActiveXObject('Microsoft.XMLDOM');
                                doc.async = false;
                                try {
                                    doc.loadXML(xdr.responseText);
                                } catch (e) {
                                    doc = undefined;
                                }
                                if (!doc || !doc.documentElement || doc.getElementsByTagName('parsererror').length) {
                                    status.code = 500;
                                    status.message = 'parseerror';
                                    throw 'Invalid XML: ' + xdr.responseText;
                                }
                                responses.xml = doc;
                            }
                        } catch (parseMessage) {
                            throw parseMessage;
                        } finally {
                            complete(status.code, status.message, responses, allResponseHeaders);
                        }
                    };
                    xdr.onerror = function () {
                        complete(500, 'error', {
                            text: xdr.responseText
                        });
                    };
                    xdr.open(options.type, options.url);
                    //xdr.send(userOptions.data);
                    xdr.send();
                },
                abort: function () {
                    if (xdr) {
                        xdr.abort();
                    }
                }
            };
        }
    });
};

jQuery.support.cors = true;

var composite = {
    BoolValue: true,
    StringValue: "Hello "
};

$.ajax({
    type: "POST",
    contentType: "application/json",
    url: targetURL,
    data: JSON.stringify(composite),
    datatype: "jsonp",
    //crossDomain :true,
    success: function (data) {
        alert("success!!::" + JSON.stringify(data));
    },
    error: function (xhr, status, error) {
        alert("error!!::" + JSON.stringify(xhr));
    }
});
于 2013-06-11T13:18:44.830 回答