uni-app的H5端怎么使用uni.request发送post请求
时间: 2023-12-23 12:05:36 浏览: 146
在uni-app中,可以使用uni.request方法来发送HTTP请求,包括POST请求。下面是一个简单的示例:
```javascript
uni.request({
url: 'http://example.com/api',
method: 'POST',
data: {
name: 'John',
age: 30
},
success: function(res) {
console.log(res.data);
},
fail: function(err) {
console.log(err);
}
});
```
其中,url指定请求的地址,method指定请求方法为POST,data指定请求体中的参数,success指定请求成功时的回调函数,fail指定请求失败时的回调函数。
需要注意的是,在H5端发送POST请求时,需要在请求头中指定Content-Type为application/x-www-form-urlencoded,否则可能会导致服务器无法正确解析请求体中的参数。可以通过设置header选项来指定请求头,如下所示:
```javascript
uni.request({
url: 'http://example.com/api',
method: 'POST',
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
name: 'John',
age: 30
},
success: function(res) {
console.log(res.data);
},
fail: function(err) {
console.log(err);
}
});
```
这样就可以在uni-app的H5端使用uni.request发送POST请求了。
阅读全文
相关推荐


















