Javascript 任何模拟*同步*XDomainRequest(XDR)请求的方法
我们正在对谷歌地图地理编码API进行跨域调用。这在现代浏览器中过去和现在都运行良好,但在IE8中根本不起作用。看起来它在IE9中也会失败(部分CORS支持)。这导致包括一个XDomainRequest(XDR)来处理IE8-9。在我的独立测试中,这样做可以很好地将数据恢复到IE8中 我现在遇到的问题是,XDR只能异步工作,所以我的地理代码函数在我的XDR.onload启动之前返回 在我的搜索函数中,我调用geocode函数:Javascript 任何模拟*同步*XDomainRequest(XDR)请求的方法,javascript,jquery,json,synchronous,xdomainrequest,Javascript,Jquery,Json,Synchronous,Xdomainrequest,我们正在对谷歌地图地理编码API进行跨域调用。这在现代浏览器中过去和现在都运行良好,但在IE8中根本不起作用。看起来它在IE9中也会失败(部分CORS支持)。这导致包括一个XDomainRequest(XDR)来处理IE8-9。在我的独立测试中,这样做可以很好地将数据恢复到IE8中 我现在遇到的问题是,XDR只能异步工作,所以我的地理代码函数在我的XDR.onload启动之前返回 在我的搜索函数中,我调用geocode函数: var location = Geocode(city, state)
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
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;
};
我在IE8中遇到上面的“无法确定位置”警报
以下是我的地理编码函数:
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
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;
};
如果我在geocode函数中注释掉警报(json),我会在IE8中得到结果,因为这是一个阻塞操作,所以请求有时间完成并填充我的json对象。未注释运行时,json对象不会被填充
有人知道我如何在IE中实现这一点吗?异步就是异步。如果您想在请求完成后执行某些操作,则必须将其放入xdr.onload函数中 javascript中没有“wait”函数。您可以构建一个并执行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/browser;)
jQuery文档说:
从jQuery1.8开始,对jqXHR($.Deferred)使用async:false
被弃用;您必须使用成功/错误/完成回调
选项,而不是jqXHR对象的相应方法,例如
与jqXHR.done()或不推荐使用的jqXHR.success()一样
请注意,关于jQuery1.8的最后一个片段并不意味着
async:false
已被弃用,它只是意味着如果您这样做,您就不能使用promise接口。然而,async:false
并不是一个好主意@是的,你可以。。遗憾的是,synchron请求的javascript风格不好。为什么不改为使代码异步兼容呢?它的好处是#1使用旧版,而#2在获取地理位置数据时,不会导致浏览器看起来像坏了一样。也许这就是我的空白之处,因为我尝试在onload和success回调中调用函数。该函数位于geocode中,只是在上面的geocode函数末尾有逻辑。geocode函数仍在IE中返回,没有json obj/数据,因此我的搜索函数仍在发出“无法确定位置”警报。也许我会在周末发布一个工作的fiddle/bin,然后本质上把问题转发给工作代码。