温馨提示×

UniApp中uni.request方法怎么使用

小亿
141
2023-12-23 01:28:13
栏目: 编程语言

要在UniApp中使用uni.request方法,需要先引入uni对象。

  1. 引入uni对象
import uni from 'uni-app';
  1. 使用uni.request方法发送请求
uni.request({
  url: 'http://example.com/api',
  method: 'GET',
  data: {
    key1: 'value1',
    key2: 'value2'
  },
  header: {
    'content-type': 'application/json'
  },
  success: (res) => {
    console.log(res.data);
  },
  fail: (err) => {
    console.log(err);
  }
});

在上面的示例中,我们使用了uni.request方法发送了一个GET请求,请求的URL是’http://example.com/api’,请求参数是key1=value1和key2=value2。在成功回调函数中,我们打印了返回的数据;在失败回调函数中,我们打印了错误信息。

你可以根据自己的需求修改请求方法、URL、请求参数、请求头等。

0