任何模拟* synchronous * XDomainRequest(XDR)请求的方法 [英] Any way to simulate a *synchronous* XDomainRequest (XDR) request

查看:97
本文介绍了任何模拟* synchronous * XDomainRequest(XDR)请求的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在对Google地图地理编码API进行跨域调用。在现代浏览器中,这一切都很好用,但它在IE8中根本不起作用。看起来它也会在IE9中失败(部分CORS支持)。这导致包括XDomainRequest(XDR)来处理IE8-9。这样做在我的独立测试中运行良好,以便在IE8中恢复数据。

We're making a cross domain call to the google maps geocode API. This was and is working all fine and dandy in modern browsers, but it wasn't working at all in IE8. Looks like it would fail in IE9 as well (partial CORS support). This led to including a XDomainRequest (XDR) to take care of IE8-9. Doing that worked fine in my standalone test to get data back in IE8.

我现在遇到的问题是XDR只能异步工作,所以我的地理编码功能在我之前返回xdr.onload会激活。

The problem I'm running into now is XDR only works asynchronously so my geocode function returns before my xdr.onload fires.

在我的搜索功能中,我调用地理编码功能:

In my search function, I call the geocode function:

var location = Geocode(city, state);

if (!location) {
    alert('Unable to determine the location of the city and state you entered');
    StopLoading();
    return;
}
//function then uses location.lat and location.lng coordinates

我在IE8上面点击无法确定位置警告。

I'm hitting the "Unable to determine location" alert above in IE8.

这是我的地理编码功能:

Here's my geocode function:

Geocode = function (address, state) {
var protocol = location.protocol,
    url = '//maps.googleapis.com/maps/api/geocode/json?sensor=false&address=',
    param = encodeURIComponent(address + ', ' + state),
    json = {};

if ('XDomainRequest' in window && window.XDomainRequest !== null) {
    //IEs that do not support cross domain xhr requests
    var xdr = new XDomainRequest();

    xdr.open('get', protocol + url + param);
    xdr.onload = function() {
        json = jQuery.parseJSON(xdr.responseText);
    };
    xdr.send();
} else {
    //good browsers
    jQuery.ajax({
        url: protocol + url + param,
        type: 'get',
        dataType: 'json',
        async: false,
        success: function(data) {
            json = data;
        }
    });
}

//alert(json);
if (json.status !== 'OK') {
    alert('Unable to determine the location of the city and state you entered');
    return null;
}

return json.results[0].geometry.location;
};

如果我在地理编码功能中注释掉警报(json),我会在IE8中得到我的结果,因为这是一个阻塞操作,所以请求有时间完成并填充我的json对象。如果它没有注释运行,则不会填充json对象。

If I comment out the alert(json) in the geocode function, I get my results in IE8 because that's a blocking operation so the request has time to finish and populates my json object. When it's run uncommented, the json object isn't populated.

任何人都有任何想法如何在IE中使用它?

Anyone have any ideas how I can get this working in IE?

推荐答案

asynchron是异步的。如果您想在请求完成后执行某些操作,则必须将其放入xdr.onload函数中。

asynchron is asynchron. If you want to do something after the request is finished u have to put it into the xdr.onload function.

javascript中没有等待功能。你可以构建一个并执行一个setTimeout循环来检查所有x-miliseconds的变量。但在这种情况下,这对你没有帮助(而且非常难看)。
在你的情况下,你可以使用onerror和ontimeout来检查服务器是否有问题,并使用onload来检查城市是否在json中。

There is no "wait" function in javascript. You could build one and do a setTimeout loop to check the variable all x-miliseconds. But that would not help u in this case (and its very ugly). In your case you can use the onerror and ontimeout to check if the server had a problem, and the onload to check if the city is in the json.

xdr.onload = function() {
json = jQuery.parseJSON(xdr.responseText);
//check if a city is loaded, go on if true
};
xdr.onerror = function() {
alert('Unable to determine the location of the city and state you entered');
//do whatever u wanna do if something went wrong
xdr.ontimeout = function() {
alert('404 Server');
//do whatever u wanna do if something went wrong
}

i希望这可以帮助你找到方法(顺便说一句,使用异步请求是一个更好的方法,然后阻止漏洞javascript /浏览器;)

i hope this helps you find the way (and by the way, the use of async requests is a much a better way then block the hole javascript/browser ;)

jQuery doc说:

The jQuery doc says:


从jQuery 1.8开始,不推荐使用async:false和jqXHR($ .Deferred)
;你必须使用success / error / complete回调
选项而不是jqXHR对象的相应方法,例如
作为jqXHR.done()或不推荐使用的jqXHR.success()

As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success()

这篇关于任何模拟* synchronous * XDomainRequest(XDR)请求的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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