2012-04-30 25 views
1

試圖爲IE 7用戶找到解決辦法。基本上在我的客戶端JavaScript應用程序中,以下代碼向運行node.js的服務器發出httprequest如果客戶端使用IE8,我會獲得成功的連接,但在IE7中卻不成功。思考?HTTPRequest到節點服務器工作在IE 8但不是IE 7

var myxmlhttp; 
doRequest(); 

function doRequest() { 
    var url = "http://someserver:8000/" + username; 
    myxmlhttp = CreateXmlHttpReq(resultHandler); 

    if (myxmlhttp) { 
     XmlHttpGET(myxmlhttp, url); 
    } else { 
     alert("An error occured while attempting to process your request."); 
     // provide an alternative here that does not use XMLHttpRequest 
    } 
} 

function resultHandler() { 
    // request is 'ready' 
    if (myxmlhttp.readyState == 4) { 
     // success 
     if (myxmlhttp.status == 200) { 
      alert("Success!"); 
      // myxmlhttp.responseText is the content that was received 
     } else { 
      alert("There was a problem retrieving the data:\n" + req.status.text); 
     } 
    } 
} 

function CreateXmlHttpReq(handler) { 
    var xmlhttp = null; 

    if (window.XMLHttpRequest) { 
     xmlhttp = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) { 
     // users with activeX off 
     try { 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
     } catch (e) {} 
    } 

    if (xmlhttp) xmlhttp.onreadystatechange = handler; 

    return xmlhttp; 
} 

// XMLHttp send GEt request 
function XmlHttpGET(xmlhttp, url) { 
    try { 
     xmlhttp.open("GET", url, true); 

     xmlhttp.send(null); 
    } catch (e) {} 
} 
+0

在IE7從我可以告訴的,這個問題似乎是在xmlhttp.open(「GET」 ,url,true);我不確定接下來要做什麼 – DaBears

回答

0

不知道,但你需要調整CreateXmlHttpReq函數來處理不同類型的微軟ActiveXObjects

function CreateXmlHttpReq(handler) { 
    var xmlhttp = null; 

    if (window.XMLHttpRequest) { 
     xmlhttp = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) { 
     var types = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0", "Microsoft.XMLHTTP"]; 

     for (var i = 0; i < types.length; i++) { 
      try { 
       xmlhttp = new ActiveXObject(types[i]); 
       break; 
      } catch(e) {} 
     } 
    } 

    if (xmlhttp) { 
     xmlhttp.onreadystatechange = handler; 
    } 

    return xmlhttp; 
} 
+0

嗨SilentSakky-我試過你的方法,但是我沒有在任何運行IE7的機器上獲得成功返回。想法? – DaBears