First, encapsulate AJAX packaging the most rigorous AJAX, can directly copy the reference AJAX.js

function sendAjax(obj){

If (!!!!!!!!!) Obj. Url) {/ not preach throw new Error (' address required)/cast by definition Error} / incoming whether need to validate the url string after the if (Object. The prototype. ToString. Call (obj. Url)! == '[Object String]'){throw new Error(' address must be a String ')} / If (! Obj.method){obj.method = 'get'} / pass in - check if(obj.method.tolowerCase ()! =='get' && obj.method.toLowerCase()! ==' POST '){throw New Error(' Request must be GET or POST ')} / Assign if(obj.async === undefined){obj.async = true} / Assign if(obj.async === undefined) if(Object.prototype.toString.call(obj.async) ! == '[Object Boolean]'){throw new Error('async must be a Boolean ')} / pass success if(! Obj. Success) {obj. Success = function () {}} / judge whether function if (Object. The prototype. ToString. Call (obj. Success! == '[object Function]'){throw new Error(' Success must be a Function ')} if(! obj.error){ obj.error = function(){} } if(Object.prototype.toString.call(obj.error) ! == '[object Function]'){throw new Error(' Error must be a Function ')} / var STR = '; If (obj. Data) {/ judgment type if (Object. The prototype. ToString. Call (obj. Data) = = = '[Object String]') {STR = obj. Data} the else If (Object. The prototype. ToString. Call (obj. Data) = = = '[Object Object]') {/ Object to be processed into string var f = ' '; For (var attr in obj. Data){STR += f + attr + '=' + obj. Data [attr] f =' &'}}else{throw new Error(' invalid data ')} If (obj.method.tolowerCase () === 'get'){obj.url += '? '+ STR}} / If (! Obj.datatype){obj.datatype = 'json'} if(obj.datatype. TolowerCase ()! =='json' && obj.dataType.toLowerCase()! =='text' && obj.dataType.toLowerCase()! XML = = ' ') {throw new Error (' dataType illegal ')} / * {url: "', method:" ', data: "', async:" ', success: the function () {}, the Error: "', dataType:'text/json/xml' } */ var xhr = new XMLHttpRequest; Xhr.onreadystatechange = function(){if(xhr.readyState === 4){if(ParseInt (xhr.statechange /100) === 2) switch(obj.dataType.toLowerCase()){ case 'json': var res = xhr.responseText; res = JSON.parse(res) break; case 'xml': var res = xhr.responseXML; break; case 'text': var res = xhr.responseText; break; } obj.success(res) }else{ obj.error() } } } xhr.open(obj.method,obj.url,obj.async) if(obj.method.toLowerCase() === 'POST' &&obj.data){/ Set header xhr.setRequestHeader(' Content-Type ',' Application/X-WWW-form-urlencoded ') xhr.send(STR){/ Set header xhr.setRequestHeader(' Content-Type ',' Application/X-WWW-form-urlencoded ') xhr.send(STR) return } xhr.send()

}

function promiseAjax(obj){

Return new Promise(function(resolve,reject){return new Promise(function(resolve,reject)){return new Promise(function(resolve,reject)); Obj. Url) {/ / not preach throw new Error (' address required)/cast by definition Error} / incoming whether need to validate the url string after the if (Object. The prototype. ToString. Call (obj. Url)! == '[Object String]'){throw new Error(' address must be a String ')} / If (! Obj.method){obj.method = 'get'} / pass in - check if(obj.method.tolowerCase ()! =='get' && obj.method.toLowerCase()! ==' POST '){throw New Error(' Request must be GET or POST ')} / Assign if(obj.async === undefined){obj.async = true} / Assign if(obj.async === undefined) if(Object.prototype.toString.call(obj.async) ! == '[Object Boolean]'){throw new Error('async must be a Boolean ')} / determine whether data is passed to var STR = "; If (obj. Data) {/ judgment type if (Object. The prototype. ToString. Call (obj. Data) = = = '[Object String]') {STR = obj. Data} the else If (Object. The prototype. ToString. Call (obj. Data) = = = '[Object Object]') {/ Object to be processed into string var f = ' '; For (var attr in obj. Data){STR += f + attr + '=' + obj. Data [attr] f =' &'}}else{throw new Error(' invalid data ')} If (obj.method.tolowerCase () === 'get'){obj.url += '? '+ STR}} / If (! Obj.datatype){obj.datatype = 'json'} if(obj.datatype. TolowerCase ()! =='json' && obj.dataType.toLowerCase()! =='text' && obj.dataType.toLowerCase()! XML = = ' ') {throw new Error (' dataType illegal ')} / * {url: "', method:" ', data: "', async:" ', success: the function () {}, the Error: "', dataType:'text/json/xml' } */ var xhr = new XMLHttpRequest; Xhr.onreadystatechange = function(){if(xhr.readyState === 4){if(ParseInt (xhr.statechange /100) === 2) switch(obj.dataType.toLowerCase()){ case 'json': var res = xhr.responseText; res = JSON.parse(res) break; case 'xml': var res = xhr.responseXML; break; case 'text': var res = xhr.responseText; break; } resolve(res) }else{ reject() } } } xhr.open(obj.method,obj.url,obj.async) if(obj.method.toLowerCase() === 'post' && Obj.data){/ Set header xhr.setRequestHeader(' Content-Type ',' Application/X-WWW-form-urlencoded ') xhr.send(STR) return} xhr.send() })

}

Technical support: WeChat brush steps