首页 > Web开发 > 详细

vue--http请求的封装--token

时间:2018-05-25 12:58:26      阅读:915      评论:0      收藏:0      [点我收藏+]
export const FetchHandler = function (url,opt) {
let paramStr = ‘‘;
let token = ‘‘;
for(key in opt){
if (opt[key] !== ‘‘){

if (typeof opt[key] === ‘object‘) {
paramStr += key + ‘=‘ + JSON.stringify(opt[key]) + ‘&‘
} else {
paramStr += key + ‘=‘ + opt[key] + ‘&‘
}
}
}
paramStr = paramStr.substring(0,paramStr.length - 1)

return new Promise((resolve,reject) => {
AsyncStorage.getItem(‘token‘,(error,result)=>{
token = result;
}).then(result=>{
fetch(url, {
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
‘token‘: token
},
body: paramStr
})
.then((response) => response.json())
.then(function (response) {
resolve(response)
})
.catch((reject)=>{
reject(‘request error‘);
})
});

})
};

vue--http请求的封装--token

原文:https://www.cnblogs.com/langqq/p/9087507.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!