XMLHttpRequest浏览器支持 [英] XMLHttpRequest Browser Support

查看:244
本文介绍了XMLHttpRequest浏览器支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码段在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

$ b $

     / * 
为Internet Explorer 5.x-6.x提供XMLHttpRequest构造函数:
其他浏览器(包括Internet Explorer 7.x-9.x)不重新定义
XMLHttpRequest如果它已经存在。

此示例基于以下发现:
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指向Msxml2.XMLHTTP并且是多余的
throw new Error(此浏览器不支持XMLHttpRequest。
};
}

  / ** 
*获取XMLHttpRequest。对于Internet Explorer 6,尝试使用MSXML 6.0,
*然后回退到MXSML 3.0。
*如果无法创建对象,则返回null。
* @return {XMLHttpRequest或等价的ActiveXObject}
* /
函数getXHR(){
if(window.XMLHttpRequest){
// Chrome,Firefox,IE7 + ,Opera,Safari
return new XMLHttpRequest();
}
// IE6
try {
//最新的稳定版本。它具有最好的安全性,性能,
//可靠性和W3C一致性。与Vista的船,并可用
//与其他操作系统通过下载和更新。
return new ActiveXObject('MSXML2.XMLHTTP.6.0');
} catch(e){
try {
//后备。
return new ActiveXObject('MSXML2.XMLHTTP.3.0');
} catch(e){
alert('此浏览器未启用AJAX。
return null;
}
}
}

参考:http://en.wikipedia.org/wiki/XMLHttpRequest http://www.webmasterworld.com/javascript/4027629.htm


Is there a reason the following snippet would not work in 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.

解决方案

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/XMLHttpRequest and http://www.webmasterworld.com/javascript/4027629.htm

这篇关于XMLHttpRequest浏览器支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆