最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - XDomainRequest vs XMLHTTPRequest - Stack Overflow

programmeradmin10浏览0评论

We are creating an application using PixiJS which has a dynamic json loader in it.

It loads the .json files using the following:

if(window.XDomainRequest)
{
    this.ajaxRequest = new window.XDomainRequest();
}
else if (window.XMLHttpRequest)
{
    this.ajaxRequest = new window.XMLHttpRequest();
}
else
{
    this.ajaxRequest = new window.ActiveXObject('Microsoft.XMLHTTP');
}

Which seems to work everywhere except on windows phone and IE. However, if I swap XMLHttpRequest with XDomainRequest it works fine.

So finally, can someone please explain the differences between XDomainRequest and XMLHTTPRequest? Which one should take precedence over the other?

We are creating an application using PixiJS which has a dynamic json loader in it.

It loads the .json files using the following:

if(window.XDomainRequest)
{
    this.ajaxRequest = new window.XDomainRequest();
}
else if (window.XMLHttpRequest)
{
    this.ajaxRequest = new window.XMLHttpRequest();
}
else
{
    this.ajaxRequest = new window.ActiveXObject('Microsoft.XMLHTTP');
}

Which seems to work everywhere except on windows phone and IE. However, if I swap XMLHttpRequest with XDomainRequest it works fine.

So finally, can someone please explain the differences between XDomainRequest and XMLHTTPRequest? Which one should take precedence over the other?

Share Improve this question asked Aug 5, 2014 at 14:42 MowdayMowday 4121 gold badge3 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 16

XDomainRequest is the only way of having an XHR that supports CORS in IE8 and 9. At the time of IE8, Microsoft decided to e up with their own CORS XHR instead of the standard CORS XMLHttpRequest which is now used in IE10. Since IE10, XDomainRequest has been removed (editor: see ment).

You should only use XDomainRequest if you need CORS in IE8/9. XDomainRequest is not pletely interchangeable with XMLHttpRequest, the interfaces aren't exactly the same. One is example is it doesn't support the onreadystatechange event. So if you want to switch between them like in the question, you will need to make sure you use onload not onreadystatechange and check any other functionality is interchangeable.

There's an example usage in this answer.

发布评论

评论列表(0)

  1. 暂无评论