2013-05-27 184 views
2

是否有一個原因,以下代碼片段不能在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

+1

你爲什麼懷疑的文件? – Quentin

+2

你在頁面中包含'json2.js'?我記得,IE 7(也可能是IE 8)沒有原生的'JSON.stringify'函數,因此我們需要包含'json2.js'(可以在這裏下載:https:// github.com/douglascrockford/JSON-js)使用'JSON'對象和你的方法。 –

+2

也許你需要澄清什麼是不工作,併爲問題做一些研究 –

回答

10

在谷歌小的搜索會爲你的基本問題提供了一個很好的答案

/* 
    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'?它依賴於平臺嗎? **編輯**:爲了回答我自己的問題,['setRequestHeader'在IE7中添加](http://msdn.microsoft.com/en-us/library/ie/ms536752%28v=vs.85%29的.aspx)。因此,這些'ActiveXObject'不支持'setRequestHeader',但在IE7及更高版本中不是必需的。 – apsillers

+1

我認爲'XMLHTTPRequest'對象是在IE7中引入的,但早期的'MSXML'支持'setRequestHeader',請查看http://help.dottoro.com/ljhcrlbv.php和http://cephas.net/blog/2003/06/17/msxml2xmlhttp40/ –

+0

啊,好研究!是的,似乎至少某些形式的ActiveX變體支持'setRequestHeader'。 – apsillers

0

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對象。

1

jQuery source code

/* 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")