【问题标题】:Any way to simulate a *synchronous* XDomainRequest (XDR) request模拟*同步* XDomainRequest (XDR) 请求的任何方式
【发布时间】:2013-08-30 21:51:38
【问题描述】:

我们正在对 google maps geocode API 进行跨域调用。这在现代浏览器中过去和现在都很好,而且在现代浏览器中运行良好,但在 IE8 中根本不起作用。看起来它在 IE9 中也会失败(部分 CORS 支持)。这导致包含一个 XDomainRequest (XDR) 来处理 IE8-9。在我的独立测试中这样做可以很好地在 IE8 中获取数据。

我现在遇到的问题是 XDR 只能异步工作,所以我的地理编码函数在我的 xdr.onload 触发之前返回。

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

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 中点击了上面的“无法确定位置”警报。

这是我的地理编码功能:

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

如果我在地理编码函数中注释掉 alert(json),我会在 IE8 中得到结果,因为这是一个阻塞操作,因此请求有时间完成并填充我的 json 对象。当它在未注释的情况下运行时,不会填充 json 对象。

有人知道如何在 IE 中使用它吗?

【问题讨论】:

  • 为什么不让你的代码异步兼容呢?它将具有 #1 在 oldie 中工作的好处,并且 #2 在获取地理位置数据时不会导致浏览器看起来像是坏了。
  • 也许这是我在画一个空白的地方,因为我尝试在我的 onload 和 success 回调中调用一个函数。该函数在地理编码中,并且只是在上述地理编码函数的末尾具有逻辑。地理编码功能仍在 IE 中返回,没有 json obj/data,因此我的搜索功能仍在点击“无法确定位置”警报。也许我会在周末发布一个工作小提琴/bin,基本上重新发布指向工作代码的问题。

标签: javascript jquery json synchronous xdomainrequest


【解决方案1】:

异步是异步的。如果你想在请求完成后做一些事情,你必须把它放到 xdr.onload 函数中。

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

我希望这可以帮助您找到方法(顺便说一下,使用异步请求是一种比阻止 javascript/浏览器漏洞更好的方法;)

jQuery 文档说:

从 jQuery 1.8 开始,使用 async: false 和 jqXHR ($.Deferred) 已弃用;您必须使用成功/错误/完成回调 选项而不是 jqXHR 对象的相应方法,例如 作为 jqXHR.done() 或已弃用的 jqXHR.success()

【讨论】:

  • 请注意,关于jQuery 1.8 的最后一个sn-p 并不意味着async: false 已被弃用,它只是意味着如果你这样做了就不能使用promise 接口。不过,async: false 并不是一个好主意!
  • @Alnitak 是的,你可以.. 遗憾的是,用 javascript 错误样式同步请求。
猜你喜欢
  • 2015-05-29
  • 2013-01-11
  • 1970-01-01
  • 2013-04-04
  • 1970-01-01
  • 2019-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多