【问题标题】:XDomainRequest onLoad not firing correctly in IE8XDomainRequest onLoad 在 IE8 中未正确触发
【发布时间】:2015-01-10 15:42:00
【问题描述】:

我正在向 google geocode api 发出请求,以获取给定邮政编码的纬度和经度。这在 IE8 之外的所有浏览器中都可以正常工作(震惊!)。

为了解决这个问题,我为 IE 实现了 XDomainRequest。

  /*
      Convert postcode to lat/lng.
  */

  var convertPostcodeToLatLng = function(postcode) {
    var isPostcodeValid = false,
      postcodeRegex = /[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}/gi,
      lat,
      lng;

    if (window.XDomainRequest) {
      // Use Microsoft XDR
      var xdr = new XDomainRequest();

      xdr.onload = function() {

        response = JSON.parse(xdr.responseText);

        if (postcode.match(postcodeRegex) && response.status === 'OK') {
          lat = response.results[0].geometry.location.lat;
          lng = response.results[0].geometry.location.lng;

          isPostcodeValid = true;
        }
      }

    } else {

      $.ajax({
        url: '//maps.googleapis.com/maps/api/geocode/json?address=' + postcode + ',+UK&sensor=false',
        type: 'get',
        dataType: 'json',
        cache: false,
        async: false,
        success: function(response) {

          window.console && console.log(response);

          if (postcode.match(postcodeRegex) && response.status === 'OK') {
            lat = response.results[0].geometry.location.lat;
            lng = response.results[0].geometry.location.lng;

            isPostcodeValid = true;
          }

        },
        error: function(response) {
          // error
        }
      });

    }

    if (!isPostcodeValid) return ['error', 'Please enter a valid postcode'];

    return [lat, lng];
  }

我是这样称呼它的

applicantPostcode = fm.find('input[name=postcode]').val(),
applicantPostcodeCoords = convertPostcodeToLatLng(applicantPostcode);

我还在继续之前检查错误

if (applicantPostcodeCoords[0] === 'error') {
      $('#sb-player #postcode').after('<label class="invalid-postcode">' + applicantPostcodeCoords[1] + '</label>');

  return;
}

现在我说它不起作用,那是个谎言,它确实起作用了,只是不能立即起作用,所以错误会在不应该发生的时候被触发。

我已经尝试在 IE8 中调试,这似乎是这样做的

  • 从表单元素获取邮政编码
  • 使用邮政编码跳转到函数
  • 创建新的 XDomainRequest
  • 跳过 xdr.onload
  • 创建新的 XDomainRequest
  • 跳出条件,下到isPostcodeValid并返回错误

现在请求确实有效,我已经对其进行了测试,问题是它不能立即工作,因此会跳入错误。

有人知道为什么它会跳过 onload 而不是进入它吗?

谢谢!

【问题讨论】:

标签: ajax google-maps internet-explorer-8 xdomainrequest xdr


【解决方案1】:

一旦你的函数加载它就会进入第一个条件并失败

   if (postcode.match(postcodeRegex) && response.status === 'OK') {
        lat = response.results[0].geometry.location.lat;
        lng = response.results[0].geometry.location.lng;

        isPostcodeValid = true;
      }

因为没有响应,因为此时您还没有发送请求?

所以当你在函数第一次运行时点击这个 if (!isPostcodeValid) return ['error', 'Please enter a valid postcode'];

【讨论】:

  • 奇怪的是它第一次跳过然后返回并返回响应,isPostcodeValid 返回 true 但它永远不会跳出条件来检查它 f (!isPostcodeValid) return ['错误', '请输入有效的邮政编码'];
  • 第二遍它击中了else?日志记录 window.XDomainRequest 在任何时候都返回 true 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多