function getURL(URL) {
return new Promise(function (resolve, reject) {
var req = new XMLHttpRequest();
req.open('GET', URL, true);
req.onload = function () {
if (req.status === 200) {
resolve(req.responseText);
} else {
reject(new Error(req.statusText));
}
};
req.onerror = function () {
reject(new Error(req.statusText));
};
req.send();
});
}
// 运行示例
var URL = "http://httpbin.org/get";
getURL(URL).then(function onFulfilled(value){
console.log(value);
}).catch(function onRejected(error){
console.error(error);
});
相关文章
- 12-02promise与async-await
- 12-02一个注意点和技巧,promise.all
- 12-02Promise里捕捉错误的最佳实践
- 12-02VueRouter路由跳转报错vue-router.esm.js?fe87:1958 Uncaught (in promise) NavigationDuplicated: Avoided redun
- 12-02Vue中重复点击相同路由,出现 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation 问题
- 12-02Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to 解决办法
- 12-02Uncaught (in promise) Error: Redirected when going from "/login" to "/home" via
- 12-02promise原理
- 12-02js之异步操作(事件循环机制及异步处理方式含promise)
- 12-02Promise