您的位置:首页 > Web前端

对Fetch进行简单的二次封装

2018-01-01 10:12 387 查看

新建
HttpUtils.js

let qs = require('qs');

export default class HttpUtils {
static get(url) {
return new Promise((resolve, reject) => {
fetch(url, {
credentials: 'include',
headers: {
'Accept': 'application/json, text/plain, */*'
}
})
.then(response => response.json())
.then(result => {
resolve(result)
})
.catch(err => {
reject(err)
})
})
}

static post(url, data) {
return new Promise((resolve, reject) => {
fetch(url, {
method: 'POST',
credentials: 'include',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: qs.stringify(data)
})
.then(response => response.json())
.then(result => {
resolve(result)
})
.catch(err => {
reject(err)
})
})

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: