uniapp网络请求获取数据_2.uni-app 发起网络请求

## 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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值