跨域请求如何在IE8中工作? [英] How do cross domain requests work in IE8?

查看:315
本文介绍了跨域请求如何在IE8中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do cross domain requests work in IE8? The target request is to

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工作的职位,并在启用cors标志在ajax设置,我能够进行调用。但是,我的回调函数从未被调用过。

I tried using $.getJson and $.ajax and they worked for IE9/10 and FF and chrome. However it would not work for IE8. I read some of the posts related to getting XHR working and after enabling the cors flag in ajax settings, I was able to make the call. However, my callback never got invoked.

我以后通过使用Google的geocoder API工作。但是,我想知道如何让这个工作没有一个API

I got it working later by using Google's geocoder API. However, I would like to know how to get this to work without an API

感谢
-Venu

Thanks -Venu

推荐答案

我也面临这个问题(不确定目标浏览器),但使用下面的代码,我得到了解决..
使用此代码,域请求

I was also facing this issue(not sure about the target browser), but using the below code, I got it resolved.. Use this code before making cross domain request

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

这篇关于跨域请求如何在IE8中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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