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'; 導入是因為下載了插件(具體可查看博客),沒有下載的需常規導入)?
?運行結果: