2011-01-05 9 views
1
if (window.XMLHttpRequest) { 
xmlhttp = new XMLHttpRequest(); 
} else { 
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //If browser == IE, get ActiveX object 
} 
    xmlhttp.open("GET", 'questions.xml', false); //Open the file using the GET routine 
    xmlhttp.send(); //Send request 
    xmlDoc = xmlhttp.responseXML; //xmlDoc holds the document information now 
} 
console.log(xmlhttp.responseText); 

このコードは何らかの理由でChrome(任意のバージョン)では動作しません。 - それは私を狂わせてしまった。ChromeでのJavascriptとXML解析

aaaand、私のXML:結果は、コードは次の文(すなわちXMLDOC =に続いたときにはまだない、すなわち

<?xml version='1.0' encoding='utf-8'?> 
<Answers> 

    <Question1 q="&lt;h2 style='font-size: 16px; line-height: 20px; color: #333333; padding-bottom: 15px;'&gt;What's the right colour?&lt;br /&gt;" a1="A) Blue" a2="B) Purple" a3="C) Green" a4="C) Red" p1="50" p2="25" p3="10" p4="15"> 
     <Answer>a3</Answer> 
    </Question1> 

</Answers> 
+0

よろしくお願いいたします。 1. XMLは受信されていますか? 'console.log(xmlhttp.responseText)'を実行した場合、どうしますか? 2.どのコンテンツタイプが送信されていますか?それは 'application/xml'ですか? – lonesomeday

+0

FirefoxとIEでは、XMLが正しく返されますが、Chromeでは何も表示されません.-console.log()行にも届かない(質問が更新されました) –

+0

どのようにコンテンツタイプを確認できますかXML? –

答えて

2

入れ:

if ($.browser.msie) { 
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //If browser == IE, get ActiveX object 
        xmlhttp.onreadystatechange=function() { 
         if(xmlhttp.readyState == 4) { 
         //alert(xmlhttp.responseText); //debugging... 
         } 
        } 
        xmlhttp.open("GET", 'questions.xml', false); //Open the file using the GET routine 
        xmlhttp.send(null); //Send request 
        xmlDoc = xmlhttp.responseXML; //xmlDoc holds the document information now 
        } else if ($.browser.firefox) { 
        xmlhttp = new document.XMLHttpRequest(); 
        xmlhttp.open("GET", 'questions.xml', false); //Open the file using the GET routine 
        xmlhttp.send(null); //Send request 
        xmlDoc = xmlhttp.responseXML; //xmlDoc holds the document information now 
        } else { 
        xmlhttp = new XMLHttpRequest(); 
        xmlhttp.open("GET", 'questions.xml', false); //Open the file using the GET routine 
        xmlhttp.send(null); //Send request 
        xmlDoc = xmlhttp.responseXML; //xmlDoc holds the document information now 
      } 

希望に役立ちます!

+0

ああ - 愛してるよ! –

1

それがChromeで、()を送ることだろうが、非同期..です)?

[編集]あなたはコードをステップ実行して(JavaScriptデバッグ)、send()リクエストの直後に一時停止することでテストできます。しばらく待ってから(リソースタブを確認してください)、続行してください。速いGoogleは、(a)同期性(link)を指定するパラメータがあることを私に教えた。

[EDIT] nevermind、試してみる価値があった。おそらく有用なもう一つの発言ですが、ここに私が見つけたコードスニペットがあります。これは、いくつかの余分な故障の報告がありますので、特に、試してみる価値がある:この条件文で

xmlhttp.onreadystatechange = function() 
    { 
     alert("Wait server..." + xmlhttp.readyState); 
     if(xmlhttp.readyState == 4) 
     { 
      if(xmlhttp.status == 200) 
      { 
       alert("Received:" + xmlhttp.responseText); 
      } 
      else  
      { 
       alert("Error: returned status code " + req.status + " " + xmlhttp.statusText); 
      } 
     } 
    }; 
    xmlhttp.open("GET", "questions.xml", true); 
    xmlhttp.send(null); 
+0

うーん、興味深い - –

+1

これは 'open'呼び出しの' false'のポイントです。 – lonesomeday