GET 請求 IE 會緩存,導致只執行一次。解決:URL 加時間戳/隨機數,或改 POST。
xhr.open("get","xxxx.aspx?_dc="+new Date().getTime(),true);
FF 等 W3C 瀏覽器嚴格區分大小寫。
錯:xhr.readystate / 正:xhr.readyState
同理:responseText, responseXML, status, onreadystatechange(全小寫)。
本地雙擊 HTML 測試時,xhr.status === 0(非 200)。判斷寫法:
if(xhr.status==200 || xhr.status==0)
Content-Type: text/xml(ASP/PHP/ASP.NET 寫法文中列出)responseXML 為 null,需手動構建:var doc = new ActiveXObject("microsoft.xmldoc"); doc.loadXML(xhr.responseText);xhr.open("post","xxxx.aspx",true);
xhr.setRequestHeader("content-type","application/x-www-form-urlencoded"); // 必須在 open 後設置
客戶端無法跨域,最佳方案:後端發起請求(參考文中 Alexa/PR 查詢實例)。
codepage=65001 + Session.CodePage=65001globalization + Response.CharsetencodeURIComponent(原文截斷,核心已覆蓋主流坑點)
測試環境:IE6, FF2。變量 xhr 代表 AJAX 對象。