uniapp request请求
Uniapp中的request请求可以使用uni.request()方法来实现。该方法的语法如下:
uni.request({ url: '', method: '', data: {}, header: {}, dataType: '', responseType: '', success: function (res) {}, fail: function (err) {}, complete: function () {}, })
其中,各个参数的含义如下:
- url:请求的地址;
- method:请求的方法,如GET、POST等;
- data:发送给服务器的数据;
- header:请求头部信息;
- dataType:服务器返回的数据类型,如json、text等;
- responseType:响应的数据类型,如arraybuffer、text等;
- success:请求成功后的回调函数;
- fail:请求失败后的回调函数;
- complete:请求完成后的回调函数,不论成功或失败都会调用。
示例代码:
uni.request({ url: 'http://www.example.com/api', method: 'POST', data: { name: '张三', age: 20 }, header: { 'content-type': 'application/json' // 默认值 }, success: function (res) { console.log(res.data); }, fail: function (err) { console.log(err); } })
以上代码发送了一个POST请求,请求地址为http://www.example.com/api,发送的数据为{name: '张三', age: 20},请求头部信息为application/json。请求成功后,打印响应数据。如果请求失败,则打印错误信息。
原文地址: https://www.cveoy.top/t/topic/0h3 著作权归作者所有。请勿转载和采集!