browser-request.js (586B)
1 2 3 module.exports = function(url){ 4 const req = new XMLHttpRequest(); 5 return new Promise(( resolve , reject )=>{ 6 req.onreadystatechange = function() { 7 if (this.readyState == 4){ 8 if(this.status == 200){ 9 let response = JSON.parse(this.response); 10 resolve(response); 11 }else{ 12 let err = JSON.parse(this.response); 13 reject(err); 14 } 15 } 16 } 17 req.onerror = function(e){ 18 reject(new Error (this.statusText) ); 19 } 20 req.open('GET',url,true); 21 req.send(null); 22 }); 23 24 }