XDomainRequest VS XMLHTT prequestVS、XDomainRequest、prequest、XMLHTT

2023-09-10 16:39:12 作者:言之凿凿.

我们正在创建使用PixiJS一个应用程序中有一个动态的JSON装载机。

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

它使用以下装载.json文件:

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');
}

这似乎只是在Windows Phone和IE无处不工作。 不过,如果我换XMLHtt prequest与XDomainRequest它工作正常。

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

所以最后,有人可以请解释XDomainRequest和XMLHTT prequest之间的区别?哪一个应该采取precedence比其他?

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

推荐答案

XDomainRequest是具有一个支持XHR的唯一途径 CORS 在IE8和9在IE8时,微软决定来的标准CORS XMLHtt prequest这是现在在IE10中使用了自己的CORS XHR代替。由于IE10,XDomainRequest已被删除。

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

您应该只使用XDomainRequest如果在IE8 / 9需要CORS。 XDomainRequest不完全互换与XMLHtt prequest,该接口是不完全一样的。一是例子是它不支持的onreadystatechange 事件。所以,如果你想要像这个问题在它们之间进行切换,你需要确保你使用的onload 不是的onreadystatechange 并检查任何其他功能是可以互换的。

You should only use XDomainRequest if you need CORS in IE8/9. XDomainRequest is not completely 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.

这里有一个例子使用本answer.

There's an example usage in this answer.