gpt4 book ai didi

javascript - 可以用 xhr.onload 替换 xhr.onreadystatechange 来进行 AJAX 调用吗?

转载 作者:IT王子 更新时间:2023-10-29 03:02:49 31 4
gpt4 key购买 nike

我只需要支持主要的现代浏览器(IE10+、FF、Chrome、Safari)

我可以做这个替换吗,因为我想简化我的代码库:

来自:

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 documentation在这方面很清楚。

澄清:

我选择不使用框架。

最佳答案

也许你看看this one看看W3C: XMLHttpRequest如果你的浏览器支持 xhr.onload 也是一样的。需要 XMLHttpRequest 2)

如果 xhr.onload 不存在,您还可以编写一个模拟 xhr.onload 的包装函数。(我认为您需要覆盖 XMLHttpRequest.prototype.onload = function(args){//calling onreadystatechanges somehow})。如果您只支持使用 xhr.onload 的现代浏览器,那么最好的解决方案是使用 框架(例如 ,它为此提供了包装功能。

关于javascript - 可以用 xhr.onload 替换 xhr.onreadystatechange 来进行 AJAX 调用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14946291/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com