eeui的post、get請求封裝

<script>
const stream = weex.requireModule('stream');
const storage = weex.requireModule('storage')
// const jbtoken = storage.getItem("teacherToken",(res)=>{ return res.data})


// 若是你須要經過 POST 發送 json 數據, 須要將 Content-Type 設爲 application/json。json

 


let get = function(url,token){weex

return new Promise( function(resolve,reject ) {
stream.fetch({
method: 'get',
// timeout: 30000,//30s
url:url,
type: "json",
headers: {"Content-Type":"application/json","token":token}
}, function(ret){
if(!ret.ok){
let err = new Error('請求失敗')
reject(err);
}else{
resolve(ret.data);
}
})
});

}
let post = function({url,token=null,param={}}){
return new Promise( function(resolve,reject ) {
stream.fetch({
method: 'post',
// timeout: 30000,//30s
url:url,
type: "json",
headers:{"Content-Type":"application/json","token":token},
// 參數是字符串 不能發送json
body: JSON.stringify(param)
}, function(ret){
// console.log(ret)
if(!ret.ok){
let err = new Error('請求失敗')
reject(err);
}else{
resolve(ret.data);
}

})
});


}app

export default {
get,
post
}
</script>post

相關文章
相關標籤/搜索