【React】知识点归纳:axios、Fetch的相关API
程序员文章站
2022-07-02 18:47:09
...
Axios
GET 请求
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
POST请求
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone' })
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
Fetch
GET 请求
fetch(url).then(function(response) {
return response.json()
}).then(function(data) {
console.log(data)
}).catch(function(e) {
console.log(e)
});
POST请求
fetch(url, {
method: "POST", body: JSON.stringify(data), }).then(function(data) {
console.log(data)
}).catch(function(e) {
console.log(e)
})