XMLHttpRequest浏览器支持

3
以下代码在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));

从文档上看,new XMLHttpRequest()应该会起作用,但我有些疑虑,因为我无法测试它(只能在兼容模式下),所以也许最好使用new ActiveXObject


1
你为什么怀疑文档? - Quentin
2
你在页面中包含了 json2.js 吗?据我记得,IE 7(也许还有 IE 8)没有原生的 JSON.stringify 函数,因此我们需要包含 json2.js(可以在这里下载:https://github.com/douglascrockford/JSON-js),以便使用 JSON 对象和其方法。 - Fernando Jorge Mota
2
也许你需要澄清哪些地方出了问题,并对这个问题进行一些研究。 - Saket Patel
你可以从modern.ie获取一个带有IE7的虚拟机,并用它进行测试- https://www.modern.ie/en-us/virtualization-tools#downloads - Pēteris Caune
3个回答

11

在谷歌上进行简单搜索可以为您的基本问题提供良好的答案。

/*
   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/XMLHttpRequesthttp://www.webmasterworld.com/javascript/4027629.htm


1
这些 ActiveXObject 对象支持 setRequestHeader 吗?它是依赖于平台的吗? 编辑:为了回答我的问题,在 IE7 中添加了 setRequestHeader。因此,这些 ActiveXObject 不支持 setRequestHeader,但在 IE7 及以上版本中不需要它们。 - apsillers
1
我认为XMLHTTPRequest对象是在IE7中引入的,但早期的MSXML支持setRequestHeader,请查看http://help.dottoro.com/ljhcrlbv.php和http://cephas.net/blog/2003/06/17/msxml2xmlhttp40/。 - Saket Patel
啊,好的研究!是的,看起来至少某些形式的ActiveX变体支持setRequestHeader - apsillers
1
谷歌把我带到这里。 - Goose

1

来自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.
 */

因此在IE7中最好这样使用ActiveXObject

new window.ActiveXObject("Microsoft.XMLHTTP")

0

Microsoft的示例几乎与您在代码中所做的完全相同:

var oReq = new XMLHttpRequest();
oReq.open("POST", sURL, false);
oReq.setRequestHeader("Content-Type", "text/xml");
oReq.send(sRequestBody);

从这里开始,我所能想象的唯一可能出现问题的点就是支持特定的 application/x-www-form-urlencoded值的Content-Type存在缺陷,但我非常怀疑这是一个实际存在的问题。
还要记得包含一个 JSON 库,因为 IE7 不包含本地的 JSON 对象。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接