Can one replace xhr.onreadystatechange with xhr.onload for AJAX calls?(可以用 xhr.onload 替换 xhr.onreadystatechange 来进行 AJAX 调用吗?)
问题描述
我只需要支持主流的现代浏览器(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:
发件人:
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 的包装函数.(我认为你需要重写 XMLHttpRequest.prototype.onload = function(args){//calling onreadystatechanges不知何故}
).如果您只支持使用 xhr.onload 的现代浏览器,最好的解决方案是使用 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 调用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:可以用 xhr.onload 替换 xhr.onreadystatechange 来进行 AJAX 调用吗?


- Fetch API 如何获取响应体? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01