uniapp中使用axios
时间: 2023-10-21 10:05:35 浏览: 110
axios携带cookie配置详解(axios+koa)
在uniapp中使用axios,首先需要使用npm工具安装axios组件,并且指定版本。如果不指定版本,默认会安装最新版本。安装命令如下:`npm i axios@0.27.2`。接下来,需要创建并配置axios.js文件。在该文件中,我们需要解决uniapp适配axios请求时可能出现的报错"adapter is not a function"的问题。具体的解决方法如下所示:
```javascript
import axios from 'axios'
axios.defaults.adapter = config => {
return new Promise((resolve, reject) => {
let settle = require('axios/lib/core/settle');
let buildURL = require('axios/lib/helpers/buildURL');
uni.request({
method: config.method.toUpperCase(),
url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer),
header: config.headers,
data: config.data,
dataType: config.dataType,
responseType: config.responseType,
sslVerify: config.sslVerify,
complete: function complete(response) {
response = {
data: response.data,
status: response.statusCode,
errMsg: response.errMsg,
header: response.header,
config: config
};
settle(resolve, reject, response);
}
})
})
}
```
最后,需要创建一个接口配置的js文件。完成以上工作后,就可以在uniapp中使用axios进行网络请求了。可以通过测试来验证是否配置成功。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [第一章: uniapp引入axios异步框架](https://blog.csdn.net/qq_50661854/article/details/130222481)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [uniapp 使用axios](https://blog.csdn.net/wcdunf/article/details/125394205)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文