Axios

Axios是一个基于Promise的HTTP库,可以用在浏览器和node.js中

安装方法

1
yarn add axios

GET方法

1
2
3
4
axios
.get(URL)
.then(response => (this.info = response))
.catch(err => console.log(err))

GET方法传递参数的格式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 直接在 URL 上添加参数 ID=12345
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

// 也可以通过 params 设置参数:
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

POST方法

1
2
3
4
5
6
axios
.post('https://www.runoob.com/try/ajax/demo_axios_post.php')
.then(response => (this.info = response))
.catch(function (error) { // 请求失败处理
console.log(error);
});

POST 方法传递参数格式如下:

1
2
3
4
5
6
7
8
9
10
axios.post('/user', {
firstName: 'Fred', // 参数 firstName
lastName: 'Flintstone' // 参数 lastName
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

Axios API

可以通过向axios传递相关配置来创建请求