csdnceshi62 2015-02-13 15:44 采纳率: 100%
浏览 41

IE 11 XHR故障

My company has developed an embeddable javascript video player. Part of the bootstrapping process involves using XMLHttpRequest to fetch a resource from our server. This is a cross-origin request because our video player is embedded on other people's sites via a script tag. We have our CORS headers configured correctly and it working in Chrome, Firefox, Safari, iOS, Android, and others. It also works for me when I test in IE 11 and 10.

However, from our analytics data, we are seeing the xhr object raise the error event about 10% of the time in IE 11. I have not been able to reproduce the issue myself.

Can anybody think of any reasons why this might be happening? The best theory I have is some kind of corporate IE security settings are blocking our cross-origin request. But that's just wild speculation.

Has anybody else ever experienced something like this and found a cause?

Here is the code I wrote for making the requests. I don't believe I'm doing anything wrong.

function makeRequest(config) {
  var xhr = new window.XMLHttpRequest();
  var deferred = q.defer();
  var url;

  function setHeaders(headers) {
    var header;

    for (header in headers) {
      xhr.setRequestHeader(header, headers[header]);
    }
  }

  // toQueryParams() converts an object to URL query params
  url = config.url + toQueryParams(config.params);

  /*
  * This function gets called when the XHR is "loaded". It then fulfills or rejects
  * the promise based on the HTTP status code of the response.
  */
  function handleResponse() {
    var response = {
      status: xhr.status,
      data: (function() {
        try {
          return JSON.parse(xhr.responseText);
        } catch(e) {
          return xhr.responseText;
        }
      }()),
      headers: function() {
        return xhr.getAllResponseHeaders();
      }
    };
    var status = response.status,
    var success = !(status >= 400 && status < 600);

    deferred[success ? 'resolve' : 'reject'](response);
  }

  /*
  * This function gets called when the XHR emits an "error".
  *
  * There is code elsewhere that sends these errors to our Google Analytics
  * account. This is how we know about the IE 11 XHR errors even though I
  * can't reproduce them.
  */
  function handleError() {
    deferred.reject({
      status: null,
      data: new Error('The XHR request to [' + url + '] has failed...'),
      headers: function() {
        return null;
      }
    });
  }

  xhr.onload = handleResponse;
  xhr.onerror = handleError;

  xhr.open(config.method, url, true);

  xhr.responseType = config.responseType || '';
  setHeaders(config.headers);
  xhr.timeout = config.timeout || 0;

  xhr.send(config.data);

  return deferred.promise;
}

Thanks for any help you can provide!

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33709219 2016-07-21 04:45
    关注

    Not sure if this is still relevant but your problem sounds suspiciously like - this

    I am also facing the exact same issue. In my case, this happens when IE aborts the request and the request doesn't hit the server. In this case, the xhr.status is 0 and response is empty, so parsing the JSON will throw an exception.

    I could not find a foolproof fix to this, but my workaround is to retry the calls a couple of time with same parameters/payload in the error block if the response matched the IE abort scenario.

    Cheers!

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?