1
我只是簡單地試圖在這裏遍歷XHTML中的所有div元素http://www.w3.org/。我將HTML複製到w3c.html並運行以下代碼。 但是responseText沒有完成,因爲我的代碼失敗了。使用POST請求但仍然響應文本不完整。爲什麼?
我最初使用GET請求,我認爲這是問題(認爲這是一個問題),但它仍然沒有解決問題。
function traverseHtml()
{
var xmlhttprequest=new XMLHttpRequest();
xmlhttprequest.open('POST','w3c.html',false);
xmlhttprequest.send(null);
var respText=xmlhttprequest.responseText;
alert('The ResponseText is '+respText);
/*var xml=(new DOMParser()).parseFromString(respText,'text/xml');
var result=xml.evaluate('//div',xml,null,XPathResult.ANY_TYPE,null);
var node=result.iterateNext();
alert(node);
while(node)
{
alert(node.childNodes[0].nodeName);
node=result.iterateNext()
}
*/
}
但是我使用同步機制(false)。它不應該等待open()返回? – hariom 2012-04-23 19:05:11
它應該,但使用異步機制會更好(特別是因爲它允許您檢查進度而不是凍結瀏覽器直到完成)。 – Andrea 2012-04-23 19:13:21
您是否嘗試運行您的代碼,導致responseText在firefox上仍然不完整。此外,我必須使用狀態代碼0而不是200。 https://bugzilla.mozilla.org/show_bug.cgi?id=331610 – hariom 2012-04-23 19:20:31