uniapp中如何发送get请求
时间: 2023-08-08 15:10:41 浏览: 142
在uniapp中发送GET请求可以使用uni.request方法。以下是一个示例代码:
```javascript
// 发送GET请求
uni.request({
url: 'https://api.example.com/getData',
method: 'GET',
success: function(res) {
console.log('请求成功', res.data);
},
fail: function(err) {
console.log('请求失败', err);
}
});
```
在上面的示例中,我们使用了uni.request方法发送了一个GET请求。其中,url参数指定了请求的地址,method参数指定了请求方法为GET。成功时,会执行success回调函数,将返回的数据打印到控制台;失败时,会执行fail回调函数,将错误信息打印到控制台。
你可以根据自己的需求修改url和相应的回调函数,以实现你想要的功能。
相关问题
uniapp发送get请求并且携带token
可以使用uni.request()方法发送get请求,并在header中携带token。具体代码如下:
uni.request({
url: 'http://example.com/api',
method: 'GET',
header: {
'Authorization': 'Bearer ' + token
},
success: function(res) {
console.log(res.data);
},
fail: function(err) {
console.log(err);
}
});
怎么在uniapp中使用网络请求
在uniapp中,可以使用uni.request()方法来发送网络请求。该方法的语法如下:
```
uni.request({
url: '',
method: '',
data: {},
header: {},
success: function(res) {},
fail: function(res) {},
complete: function(res) {}
})
```
其中,参数说明如下:
- url:请求的地址
- method:请求的方法,例如 GET、POST 等
- data:请求的数据,可以是普通对象或字符串
- header:请求的头部信息,可以设置 token、Content-Type 等
- success:请求成功后的回调函数
- fail:请求失败后的回调函数
- complete:请求完成后的回调函数(无论成功或失败都会调用)
示例代码如下:
```
uni.request({
url: 'https://jsonplaceholder.typicode.com/todos/1',
method: 'GET',
success: function(res) {
console.log(res.data);
},
fail: function(res) {
console.log(res.errMsg);
}
})
```
以上示例代码发送了一个 GET 请求,请求地址为 https://jsonplaceholder.typicode.com/todos/1,请求成功后会在控制台输出响应数据。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)