XMLHttpRequest浏览器支持
是否有一个原因,以下代码片段不能在IE7中工作?XMLHttpRequest浏览器支持
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));
从它似乎像new XMLHttpRequest()
应该工作,但有疑问,因为我无法测试它的文档(仅在兼容模式下),所以也许我更好地利用new ActiveXObject
。
在谷歌小的搜索会为你的基本问题提供了一个很好的答案
/*
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.");
};
}
或
/**
* 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;
}
}
}
编号:http://en.wikipedia.org/wiki/XMLHttpRequest和http://www.webmasterworld.com/javascript/4027629.htm
那些'ActiveXObject'对象是否支持'setRequestHeader'?它依赖于平台吗? **编辑**:为了回答我自己的问题,['setRequestHeader'在IE7中添加](http://msdn.microsoft.com/en-us/library/ie/ms536752%28v=vs.85%29的.aspx)。因此,这些'ActiveXObject'不支持'setRequestHeader',但在IE7及更高版本中不是必需的。 – apsillers
我认为'XMLHTTPRequest'对象是在IE7中引入的,但早期的'MSXML'支持'setRequestHeader',请查看http://help.dottoro.com/ljhcrlbv.php和http://cephas.net/blog/2003/06/17/msxml2xmlhttp40/ –
啊,好研究!是的,似乎至少某些形式的ActiveX变体支持'setRequestHeader'。 – apsillers
的example from Microsoft也几乎什么你做在您的代码中:
var oReq = new XMLHttpRequest();
oReq.open("POST", sURL, false);
oReq.setRequestHeader("Content-Type", "text/xml");
oReq.send(sRequestBody);
从这里,我甚至可以想象的唯一可能的失败点将是支持Content-Type
的具体application/x-www-form-urlencoded
值的错误,我非常认真地怀疑这是一个现存问题。
还请记住包含一个JSON库,因为IE7不包含本地JSON
对象。
/* 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.
*/
所以在IE7更好地利用ActiveXObject
这样的:
new window.ActiveXObject("Microsoft.XMLHTTP")
你为什么怀疑的文件? – Quentin
你在页面中包含'json2.js'?我记得,IE 7(也可能是IE 8)没有原生的'JSON.stringify'函数,因此我们需要包含'json2.js'(可以在这里下载:https:// github.com/douglascrockford/JSON-js)使用'JSON'对象和你的方法。 –
也许你需要澄清什么是不工作,并为问题做一些研究 –