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

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.kler.cn/a/553584.html

相关文章:

  • 985硕研一无人机方向转嵌入式可能吗?如何选择未来方向?
  • 基于Ollama本地模型DeepSeek实现RAG
  • 【Linux AnolisOS】配置Linux固定ip地址。然后在Windows上连接使用linux中docker容器里的redis和nacos。
  • 【Docker】百度网盘:基于VNC的Web访问及后台下载
  • 机器视觉中的3D高透明工件检测
  • 【大学生职业规划大赛备赛PPT资料PDF | 免费共享】
  • Fastgpt学习(5)- FastGPT 私有化部署问题解决
  • MyBatis 实现批量查询操作:以苍穹外卖套餐菜品关联查询为例
  • 斐波那契数列模型:在动态规划的丝绸之路上追寻斐波那契的足迹(上)
  • 教学资料档案管理系统
  • Navicat Premium17 连接Oracle出现 “未加载 Oracle库
  • rnn.pad_sequence测试
  • 游戏引擎学习第110天
  • 在Vue项目中使用three.js在前端页面展示PLY文件或STL文件
  • 【Docker】《一文深度解析Docker核心技术:镜像、存储卷与网络架构原理》
  • itemgetter() 是 Python operator 模块中的一个函数,主要用于从 字典、列表、元组等数据结构中取值
  • Redis_基础
  • Jeesite5:Star24k,Spring Boot 3.3+Vue3实战开源项目,架构深度拆解!让企业级项目开发效率提升300的秘密武器
  • ubuntu22.04安装tigervnc使用vnc进行访问
  • 《Python在数据可视化中的应用与实践》