一个人能代替xhr.onreadystatechange与xhr.onload对AJAX调用? [英] Can one replace xhr.onreadystatechange with xhr.onload for AJAX calls?

查看:997
本文介绍了一个人能代替xhr.onreadystatechange与xhr.onload对AJAX调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to support major modern browsers only (IE10+, FF, Chrome, Safari)

我可以让这个替代,因为我想简化我的code群:

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文档显然在这方面的工作。

I don't feel that the MDN documentation is clear in this regard.

澄清:

我选择不使用一个框架。

I choose not to use a framework.

推荐答案

也许你看看<一href="http://stackoverflow.com/questions/9181090/does-onload-equal-to-readystate-4-in-xmlhtt$p$pquest">this 之一一看 W3C:XMLHtt prequest 这是相同的,如果您的浏览器支持xhr.onload。需要XMLHtt prequest 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如果不是present。 (我想你需要重写 XMLHtt prequest.prototype.onload =功能(参数){//调用onreadystatechanges莫名其妙} )。如果你只支持使用xhr.onload应提供现代的浏览器 - 最好的解决办法是使用框架(如的 =标记jQuery的显示问题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.onreadystatechange与xhr.onload对AJAX调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆