多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## uni.request(OBJECT) 发起网络请求。 **OBJECT 参数说明** ![](https://box.kancloud.cn/a90bf284df069eddde4019c04db7d627_861x475.png) **success 返回参数说明** ![](https://box.kancloud.cn/10d44a6d100bb3833b22f2d41e85d8eb_861x165.png) **data 数据说明** 最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String。转换规则如下: 对于 GET 方法,会将数据转换为 query string。例如 { name: 'name', age: 18 } 转换后的结果是 name=name&age=18。 对于 POST 方法且 header['content-type'] 为 application/json 的数据,会进行 JSON 序列化。 对于 POST 方法且 header['content-type'] 为 application/x-www-form-urlencoded 的数据,会将数据转换为 query string。 示例 ``` uni.request({ url: 'https://www.example.com/request', //仅为示例,并非真实接口地址。 data: { name: 'name', age: 18 }, header: { 'custom-header': 'hello' //自定义请求头信息 }, success: function (res) { console.log(res.data); } }); ``` 返回值 返回一个 requestTask 对象,通过 requestTask,可中断请求任务。 ![](https://box.kancloud.cn/48b7aa1792aad7e32fffd133edf826ee_861x86.png) 示例 ``` const requestTask = uni.request({ url: 'https://www.example.com/request', //仅为示例,并非真实接口地址。 data: { name: 'name', age: 18 }, success: function (res) { console.log(res.data); } }); // 中断请求任务 requestTask.abort(); ``` Tips 请求的 header 中 content-type 默认为 application/json。