前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >XMLHttpRequest之onreadystatechange

XMLHttpRequest之onreadystatechange

作者头像
Yiiven
发布2022-12-15 15:16:02
4450
发布2022-12-15 15:16:02
举报
文章被收录于专栏:怡文菌怡文菌

XMLHttpRequest之onreadystatechange

代码语言:javascript
复制
function doReq(url, fn){
    var req = new XMLHttpRequest();
    req.onreadystatechange = function(){
        if(this.readyState == XMLHttpRequest.DONE){
            return fn(this);
        }
    }
    req.open('GET', url, false);
    req.send();
}
var res;
function myreq(xhttp){
    try{
        switch(xhttp.status){
            case 200:
                res = xhttp.responseText;
                break;
            case 404:
                throw new Error('404');
                break;
            case 500:
                throw new Error('500');
                break;
            default:
                throw new Error(xhttp.status);
                break;
        }
    }catch(e){
        res = e.message;
    }
}
doReq('https://www.w3school.com.cn/demo/js/ajax_info.txt', myreq);
console.log(res);

本文采用 「CC BY-NC-SA 4.0」创作共享协议,转载请标注以下信息:

原文出处:Yiiven https://cloud.tencent.com/developer/article/2193263

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-12-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档