axios $http get 设置responseType
时间: 2023-10-01 07:11:23 浏览: 98
在使用 axios 发起 GET 请求时,可以通过设置 `responseType` 来指定服务器返回数据的类型。常见的 `responseType` 包括:
- `arraybuffer`:返回的数据为 ArrayBuffer 类型
- `blob`:返回的数据为 Blob 类型
- `document`:返回的数据为 Document 类型
- `json`:返回的数据为 JSON 对象
- `text`:返回的数据为字符串
代码示例:
```javascript
axios.get('/api/data', {
responseType: 'json'
}).then(response => {
console.log(response.data);
});
```
相关问题
axios 怎么设置responseType
在使用axios发送请求时,我们可以通过设置`responseType`参数来指定服务器返回数据的格式。`responseType`支持以下格式:
- `arraybuffer`:返回一个包含二进制数据的ArrayBuffer对象;
- `blob`:返回一个包含二进制数据的Blob对象;
- `document`:返回一个HTML Document对象;
- `json`:返回一个JSON格式的对象;
- `text`:返回一个文本字符串。
示例代码如下:
```javascript
axios.get('/api/data', {
responseType: 'json' // 指定返回数据的格式为json
})
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
```
在上面的示例中,我们通过设置`responseType`参数为`json`,来告诉axios服务器返回的数据格式是JSON格式。这样在请求成功后,我们就可以通过`response.data`属性来获取JSON格式的数据了。
axios怎么设置responsetype
可以通过在axios请求中设置responseType属性来设置响应类型。例如,如果要将响应类型设置为JSON,则可以将responseType属性设置为'json'。
示例代码:
```
axios.get('/api/data', {
responseType: 'json'
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
```
在上面的示例中,我们将响应类型设置为JSON,并在响应中使用response.data访问响应数据。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)