当前位置: 首页 > news >正文

uni-app发起网络请求的三种方式

uni.request(OBJECT) 

发起网络请求 

具体参数可查看官方文档uni-app

data:请求的参数;

header:设置请求的 header,header 中不能设置 Referer;

method:请求方法;

timeout:超时时间,单位 ms(默认60000);

dataType:如果设为 json,会对返回的数据进行一次 JSON.parse,非 json 不会进行 JSON.parse;

....

免费测试api接口:https://jsonplaceholder.typicode.com/ 

第一种

<template></template><script setup>function request(){uni.request({url:"https://jsonplaceholder.typicode.com/posts", //开发者服务器接口地址method:"post",   //请求方法success:res=>{   //收到开发者服务器成功返回的回调函数console.log(res);}})}request();</script><style lang="scss" scoped></style>

第二种

<template></template><script setup>function request(){uni.request({url:"https://jsonplaceholder.typicode.com/posts"}).then(res=>{arrs.value=res.data})}request();</script><style lang="scss" scoped></style>

第三种

 异步同步化

<template></template><script setup>async function request(){let res = await uni.request({url:"https://jsonplaceholder.typicode.com/posts"})arrs.value=res.data}request();</script><style lang="scss" scoped></style>

示例:

<template><view class="out" v-for="item in arrs"><view class="title">{{item.title}}</view><view class="content">{{item.body}}</view></view>
</template><script setup>let arrs = ref();function request(){uni.request({url:"https://jsonplaceholder.typicode.com/posts"}).then(res=>{arrs.value=res.data})}request();
</script><style lang="scss" scoped>.out{padding: 30rpx;.title{font-size: 40rpx;}.content{margin: 15rpx 0;border-bottom: 1px solid #696969;color: #696969;}}       
</style>

( 注:ref 没有使用 import { ref } from 'vue'; 导入是因为下载了插件(具体可查看博客),没有下载的需常规导入) 

 运行结果:


http://www.mrgr.cn/news/91537.html

相关文章:

  • electron下载文件,弹窗选择下载路径,并通知下载进度
  • c# -01新属性-模式匹配、弃元、析构元组和其他类型
  • Linux驱动学习(二)--字符设备
  • docker修改镜像默认存储路径(基于 WSL2 的迁移方法)
  • Ubuntu20.04安装IsaacSim4.5与IsaacLab2.0
  • 2015年下半年试题二:论软件系统架构风格
  • wps中的js开发
  • Web后端 Tomcat服务器
  • 15. 三数之和(LeetCode 热题 100)
  • Flutter
  • 【亚马逊开发者账号02】终审问题SA+review_Pre-review+Doc.xlsx
  • 单元测试方法的使用
  • 力扣 跳跃游戏 II
  • 计算机视觉:卷积神经网络(CNN)基本概念(二)
  • 大模型 + cursor应用案例
  • 基于Django快递物流管理可视化分析系统(完整系统源码+数据库+详细开发文档+万字详细论文+答辩PPT+详细部署教程等资料)
  • QT移植,交叉编译至泰山派RK3566开发板,.pro文件解析
  • 计算机视觉:神经网络实战之手势识别(附代码)
  • Node-Red
  • 探讨如何加快 C# 双循环的速度效率