javascript XMLHttpRequest 浏览器支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16772568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XMLHttpRequest Browser Support
提问by Haji
Is there a reason the following snippet would not work in IE7?
以下代码段在 IE7 中不起作用是否有原因?
var http = new XMLHttpRequest();
var url = 'http://my_site.com/';
var obj = createJsonParamsObj();
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(JSON.stringify(obj));
From the documentation it seems like the new XMLHttpRequest()
should work, but have doubts since I can't test it (only in compatibility mode) so perhaps I better use new ActiveXObject
.
从文档看来它new XMLHttpRequest()
应该可以工作,但有疑问,因为我无法测试它(仅在兼容模式下)所以也许我更好地使用new ActiveXObject
.
回答by Saket Patel
a small search in google would provide a good answer for your basic problem
在谷歌中进行一次小搜索将为您的基本问题提供一个很好的答案
/*
Provide the XMLHttpRequest constructor for Internet Explorer 5.x-6.x:
Other browsers (including Internet Explorer 7.x-9.x) do not redefine
XMLHttpRequest if it already exists.
This example is based on findings at:
http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx
*/
if (typeof XMLHttpRequest === "undefined") {
XMLHttpRequest = function () {
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
catch (e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) {}
// Microsoft.XMLHTTP points to Msxml2.XMLHTTP and is redundant
throw new Error("This browser does not support XMLHttpRequest.");
};
}
or
或者
/**
* Gets an XMLHttpRequest. For Internet Explorer 6, attempts to use MSXML 6.0,
* then falls back to MXSML 3.0.
* Returns null if the object could not be created.
* @return {XMLHttpRequest or equivalent ActiveXObject}
*/
function getXHR() {
if (window.XMLHttpRequest) {
// Chrome, Firefox, IE7+, Opera, Safari
return new XMLHttpRequest();
}
// IE6
try {
// The latest stable version. It has the best security, performance,
// reliability, and W3C conformance. Ships with Vista, and available
// with other OS's via downloads and updates.
return new ActiveXObject('MSXML2.XMLHTTP.6.0');
} catch (e) {
try {
// The fallback.
return new ActiveXObject('MSXML2.XMLHTTP.3.0');
} catch (e) {
alert('This browser is not AJAX enabled.');
return null;
}
}
}
Ref : http://en.wikipedia.org/wiki/XMLHttpRequestand http://www.webmasterworld.com/javascript/4027629.htm
参考:http: //en.wikipedia.org/wiki/XMLHttpRequest和http://www.webmasterworld.com/javascript/4027629.htm
回答by Pinal
From jQuery source code:
来自jQuery 源代码:
/* Microsoft failed to properly
* implement the XMLHttpRequest in IE7 (can't request local files),
* so we use the ActiveXObject when it is available
* Additionally XMLHttpRequest can be disabled in IE7/IE8 so
* we need a fallback.
*/
So better use ActiveXObject
in IE7 like this:
所以更好地ActiveXObject
在 IE7 中使用,如下所示:
new window.ActiveXObject("Microsoft.XMLHTTP")
回答by apsillers
An example from Microsoftdoes nearly exactly what you do in your code:
来自 Microsoft 的一个示例几乎与您在代码中所做的完全相同:
var oReq = new XMLHttpRequest();
oReq.open("POST", sURL, false);
oReq.setRequestHeader("Content-Type", "text/xml");
oReq.send(sRequestBody);
From here, the only possible point of failure that I can even conceive of would be a bug in supporting the specific application/x-www-form-urlencoded
value for Content-Type
, which I very seriously doubt is an extant problem.
从这里开始,我什至可以想到的唯一可能的故障点是支持 的特定application/x-www-form-urlencoded
值的错误Content-Type
,我非常怀疑这是一个现存的问题。
Remember also to include a JSON library, because IE7 does not include a native JSON
object.
还要记住要包含一个 JSON 库,因为 IE7 不包含本机JSON
对象。