继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

ajax原理笔记

冉冉说
关注TA
已关注
手记 198
粉丝 42
获赞 194

function AJAX() {    var xhr = null;    if(window.XMLHttpRequest) {        xhr = new XMLHttpRequest();    } else {        try{            xhr = new ActiveXObject('Microsoft.XMLHttp');        }catch(e){            xhr = new ActiveXObject('msxml2.xmlhttp');        }    }    //get请求    this.get = function(url,success,fail){        xhr.open("GET", url,true);        xhr.onreadystatechange=function(){            if(xhr.readyState==4) {                if(xhr.status==200) {                    var txt = xhr.responseText;                    txt = JSON.parse(txt);                    success(txt);                } else {                    if(fail) {                        fail(xhr.status);                    }                }            }        };        xhr.send(null);    };    //post请求    this.post = function (url,param,success,fail) {        xhr.open("POST", url,true);        xhr.onreadystatechange=function(){            if(xhr.readyState==4) {                if(xhr.status==200) {                    var txt = xhr.responseText;                    txt = JSON.parse(txt);                    success(txt);                } else {                    if(fail) {                        fail(xhr.status);                    }                }            }        };        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");        xhr.send(param);    }; }//使用var ajax = new AJAX(); ajax.get("test.json",function(data){    //成功回调},function(data){//失败回调});



作者:一枚程序猿
链接:https://www.jianshu.com/p/83c4f3341bb1


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP