axios发送get请求
时间: 2023-09-02 18:11:39 浏览: 42
使用axios发送GET请求的基本语法如下:
```javascript
axios.get(url[, config])
.then(function (response) {
// 处理响应数据
console.log(response.data);
})
.catch(function (error) {
// 处理错误
console.log(error);
});
```
其中,`url`是请求地址,`config`是请求配置,可以包括请求头、请求参数等。
例如,发送一个简单的GET请求,获取服务器返回的数据:
```javascript
axios.get('/api/data')
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
```
在`config`中,可以设置请求参数,例如:
```javascript
axios.get('/api/data', {
params: {
id: 123,
name: '张三'
}
})
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
```
这里设置了请求参数`id`和`name`,它们会被自动拼接到url后面作为查询字符串发送给服务器。