fetch技术使用

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function status(response) {
if (response.status >= 200 && response.status < 300) {
return Promise.resolve(response)
} else {
return Promise.reject(new Error(response.statusText))
}
}

const fetchWrap = url => ops => fetch(url, {
method: 'post',
headers: {
'Content-type': 'application:/x-www-form-urlencoded:charset=UTF-8'
},
body: 'name=may&age=40',
...ops
})
.then(status)
.then(json)
.then(function(data) {
console.log('请求成功,JSON解析后的响应数据为:', data)
})
.catch(function(err) {
console.log('Fetch错误:' + err)
})
0%