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个回答


不确定这是否仍然有用,但是您的问题听起来像是可疑的- </ p>

我也面临着完全相同的问题。 就我而言,这是在IE中止请求且请求未到达服务器时发生的。 在这种情况下,xhr.status为0且响应为空,因此解析JSON将引发异常。</ p>

我找不到对此的万无一失的解决方案,但是我的解决方法是,如果响应与IE中止情况匹配,则在错误块中使用相同的参数/有效负载重试几次调用。</ p>

干杯!</ p>
     </ div>

展开原文

原文

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!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问