uni.request 接口传参
时间: 2024-12-07 11:12:18 浏览: 81
uni.request 接口是 uni-app 框架中用于发起网络请求的 API。它类似于微信小程序中的 wx.request 接口,但在 uni-app 中可以跨平台使用,包括微信小程序、H5、App 等。uni.request 接口传参主要通过其配置对象来实现,以下是一些常用的参数:
1. url: 请求的服务器接口地址。
2. method: 请求方式,默认为 'GET',可选值包括 'GET'、'POST'、'PUT'、'DELETE' 等。
3. data: 请求的参数,可以是对象或字符串。
4. header: 设置请求的 header,header 中不能设置 Referer。
5. dataType: 返回的数据格式,默认为 json。
6. responseType: 响应的数据类型,默认为 text。
下面是一个使用 uni.request 接口进行 GET 请求的示例:
```javascript
uni.request({
url: 'https://example.com/api', // 请求的服务器接口地址
method: 'GET', // 请求方式
data: {
key1: 'value1',
key2: 'value2'
}, // 请求的参数
header: {
'custom-header': 'value' // 设置请求的 header
},
success: (res) => {
console.log(res.data); // 打印服务器返回的数据
},
fail: (err) => {
console.error(err); // 打印错误信息
}
});
```
在这个示例中,我们使用 GET 请求方式向服务器发送请求,并传递了 key1 和 key2 两个参数。请求成功后会执行 success 回调函数,失败则会执行 fail 回调函数。
阅读全文
相关推荐




