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