問題描述
我只需要支持主流的現(xiàn)代瀏覽器(IE10+、FF、Chrome、Safari)
I need to support major modern browsers only (IE10+, FF, Chrome, Safari)
我可以做這個替換,因為我想簡化我的代碼庫:
Can I make this substitution as I want to simplify my code base:
發(fā)件人:
xhr.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
o.callback(xhr.responseText);
} else {
return false;
}
} else {
return false;
}
};
收件人:
xhr.onload = function (test) {
o.callback(xhr.responseText);
};
我不認為 MDN 文檔在這方面.
澄清:
我選擇不使用框架.
推薦答案
也許你看看 這個并查看 W3C: XMLHttpRequest 如果你的瀏覽器支持 xhr.onload 也是一樣的.需要 XMLHttpRequest 2)
maybe you take a look at this one and a look at W3C: XMLHttpRequest it's the same if your browser supports xhr.onload. Requires XMLHttpRequest 2)
如果 xhr.onload 不存在,您還可以編寫一個模擬 xhr.onload 的包裝函數(shù).(我認為你需要重寫 XMLHttpRequest.prototype.onload = function(args){//calling onreadystatechanges不知何故}
).如果您只支持使用 xhr.onload 的現(xiàn)代瀏覽器,最好的解決方案是使用 framework(如 jquery 或 mootools 提供包裝功能.
You can also write a wrapping function that emulates xhr.onload if it's not present.
(I think you need to override XMLHttpRequest.prototype.onload = function(args){//calling onreadystatechanges somehow}
). If you only support modern browsers using xhr.onload should be available - the best solution is using a framework (like jquery or mootools that provides wrapping functionality for that.
這篇關于可以用 xhr.onload 替換 xhr.onreadystatechange 來進行 AJAX 調用嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!