账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    ajax的onreadystatechange的问题
    26
    0

    自己用原生js造了一个轮子

    var Ajax = function (config) {
        var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
        xhr.dataType = config.dataType;
        xhr.responseType = config.responseType;
        xhr.open(config.method,config.url,config.async);
        // xhr.onreadystatechange = config.ondone(xhr);
        // xhr.onreadystatechange = function () {
        //     console.log( xhr )
        //     config.ondone(xhr);
        // };
        xhr.onerror = function () {
            console.info('readyState = ' + xhr.readyState)
            console.info('status = ' + xhr.status)
            config.onerror;
        }
        xhr.send(config.data);
    }

    以下为调用

        Ajax({
            url:'./',
            data:formData,
            async:'true',
            method:'POST',
            dataType:'type',
            responseType:'text',
            ondone:function (xhr) {
                console.log( xhr )
                console.log( xhr.readyState )
                },
            onerror:'',
        })

    现在的问题是onreadystatechange方法,如果按照第一个写的话congole只会出现一次readyState为1。
    如果我用第二个方法的话,是可以正常显示的readyState都能打印出来。
    是哪里写错了还是?

    1
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部