uniapp请求网络接口获取 播放视频 播放音乐 显示图片 显示文字

后台接口django API:

请求地址:http://192.168.0.105:8000/testjson/

def testjson(request):
    data = {
    'yy': 'http://m10.music.126.net/20230508153727/e449fe6e85027f3acbdcf0be160a46b6/ymusic/0f52/5252/0e52/882b491a3ee8bcf233ce98b64dd1c6e8.mp3',
    'sp': 'http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4',
    'tp': 'https://upload-images.jianshu.io/upload_images/5809200-a99419bb94924e6d.jpg',
    'wz': '猛志逸四海,骞翮思远翥。 —— 陶渊明',
    }
    return HttpResponse(json.dumps(data))

app端index.vue:

参考官网教程:video | uni-app官网

注意音乐如果播放不出来 请更换后台api中json数据中的·音乐地址链接

<template>
		<view class="flex">
			<text>{{itemList.yy}}<br/></text>
			<audio style="text-align: left" :poster="img" :src="itemList.yy" name="红日" author="李克勤" action="pause" controls></audio>
			<text>{{itemList.sp}}<br/></text>
			<video id="myVideo" :src="itemList.sp"controls></video>
			<text>{{itemList.tp}}<br/></text>
			<image style="width: 200px; height: 200px; background-color: #eeeeee;" mode="aspectFit" :src="itemList.tp"></image>
			<br/><text>{{itemList.wz}}<br/></text>
		</view>

</template>

<script>
	export default {
		data() {
			return {
				itemList: [],
				img:'https://web-assets.dcloud.net.cn/unidoc/zh/music-a.png'
			}
		},
		onLoad() {
			this.getList();
		},
		methods: {
			 getList() {
				uni.request({
			        url: 'http://192.168.0.105:8000/testjson/', 
					method:'GET',
			        success: (res) => {
			            console.log(res.data);
			             this.itemList =res.data;
						 
			        }
			    });
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个较为完整的uniapp网络请求接口封装代码: ```js // 封装请求函数 function request(url, data = {}, method = 'GET') { return new Promise((resolve, reject) => { uni.request({ url, data, method, header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + uni.getStorageSync('token') }, success: res => { // 请求成功 if (res.statusCode === 200) { resolve(res.data) } else { reject(res) } }, fail: err => { // 请求失败 reject(err) } }) }) } // 定义接口基础地址 const baseUrl = 'https://api.example.com' // 封装接口请求函数 export function login(data) { return request(`${baseUrl}/login`, data, 'POST') } export function getUserInfo(userId) { return request(`${baseUrl}/user/${userId}`) } // ... 还可以继续封装其他接口请求函数 ``` 在上面的代码中,我们在 `request` 函数中添加了一个请求头,用于传递身份验证信息。在每次请求接口时,都会携带一个名为 `Authorization` 的请求头,其值为当前用户的身份验证令牌。这样,在服务端就可以根据身份验证令牌来判断当前用户的身份了。 另外,我们定义了一个 `baseUrl` 常量,用于存储接口的基础地址。这样,在实际使用时,我们只需要在接口请求函数中指定相对地址即可。 在实际使用中,我们可以在需要发送网络请求的地方,直接引入并调用以上封装好的接口请求函数。例如: ```js import { getUserInfo } from '@/api/user' getUserInfo('123456').then(res => { console.log(res) }) ``` 以上代码会调用 `getUserInfo` 函数,向服务器请求 id 为 123456 的用户信息。请求成功后,会将返回的数据打印到控制台中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王家视频教程图书馆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值