vue--http請求的封裝--token
阿新 • • 發佈:2018-05-25
-type for async -h handler OS pro str url
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