uni.request(OBJECT)

发起网络请求。

在各个小程序平台运行时,网络相关的 API 在使用前需要配置域名白名单。 OBJECT 参数说明 method 有效值 必须大写,有效值在不同平台差异说明不同。 success 返回参数说明 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。示例
    1. uni.request({ url: 'https://www.example.com/request', //仅为示例,并非真实接口地址。 data: { text: 'uni.request' }, header: { 'custom-header': 'hello' //自定义请求头信息 }, success: (res) => { console.log(res.data); this.text = 'request success'; }});
    返回值 requestTask 对象,需要至少传入 success / fail / complete 参数中的一个。例如:
    1. var requestTask = uni.request({ url: 'https://www.example.com/request', //仅为示例,并非真实接口地址。 complete: ()=> {}});requestTask.abort();
    Promise 封装 requestTask,可中断请求任务。 requestTask 对象的方法列表 示例
    1. 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
  • headercontent-type 默认为 application/json
  • 超时时间 可以统一在 manifest.json 中配置 networkTimeout。
  • 调试跨域问题解决方案
  • https://github.com/charleslo1/weapp-cookie
  • 详情)
  • uni-app 插件市场有flyio、axios等三方封装的拦截器可用
  • -

在 GitHub 上编辑此页面!