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

uniapp 安卓端 使用axios 和 renderjs 上传 FormData 参数

<template>
	<view>
		<!--<button @click="request">调用 RenderJS 方法</button>-->
		<view v-show="false" :data="data" :change:data="requestModule.realRequest"></view>
	</view>
</template>

<script>
	import config from '@/config'
	import {
		getToken
	} from '@/utils/auth'
	export default {
		data() {
			return {
				data: {
					url: '',
					formData: "",
					randomValue: ''
				}
			}
		},
		methods: {
			request(url, formData) {
				this.data = {
					url: config.baseUrl+url,
					formData: formData,
					authorization: getToken(),
					randomValue: Math.floor(1000 + Math.random() * 9000)
				}
			},
			response(resp) {
				console.log("resp", resp);
				this.$emit('response', resp);
			}
		}
	}
</script>

<script module="requestModule" lang="renderjs">
	import axios from 'axios';
	export default {
		methods: {
			realRequest(newValue, oldValue) {
				//const obj = JSON.parse(newValue);
				if(!newValue.url) {
					return
				}
				const formData = new FormData();
				formData.append('form', JSON.stringify(newValue.formData));
				
				axios.post(newValue.url, formData, {
				  headers: {
				    'Content-Type': 'multipart/form-data', // 确保设置正确的 Content-Type
					'Authorization': newValue.authorization
				  }
				})
				.then(response => {
				  //console.log('文件上传成功:', response.data);
				  this.$ownerInstance.callMethod("response", response);
				})
				.catch(error => {
				  console.error('文件上传失败:', error);
				  //this.$ownerInstance.callMethod("response", error);
				});
				
			}
		}
	};
</script>

<style>

</style>

调用

<template>
	<view>
		<button @click="request">调用 RenderJS 方法</button>
		<requestFormData ref="requestFormData" @response="response"></requestFormData>
	</view>
</template>

<script>
	import requestFormData from '@/components/requestFormData/requestFormData.vue';
	export default {
		components: {
		    requestFormData,
		},
		data() {
			return {
				
			}
		},
		methods: {
			request() {
				this.$refs.requestFormData.request('/foundation/api/mingmenDailyMiss/edit',{'businessData':JSON.stringify({'id':'1879451746490097665','name':'蓝颜来','birth':'1888-15-12','demiseTime':'2000-11-01','buriedIn':'南阳市','introduction':'我是简介','motto':'我是座右铭'}),'continuedUseFileIds':''});
			},
			response(e){
				console.log("父组件拿到响应", e);
			}
		}
	}
</script>

<style>

</style>

相关文章:

  • 深入浅出GraphQL:现代API设计的未来
  • C转C++
  • python小项目编程-初级(5、词频统计,6、简单得闹钟)
  • 巧用GitHub的CICD功能免费打包部署前端项目
  • 易基因: ChIP-seq+DRIP-seq揭示AMPK通过调控H3K4me3沉积和R-loop形成以维持基因组稳定性和生殖细胞完整性|NAR
  • 【黑马点评】——相关文章汇总(包括实现,优化,测试和面经总结)
  • 亚马逊新规木炭/火柴/打火机政策SOR/2016-178/182/187标准检测流程:
  • 跟着 Lua 5.1 官方参考文档学习 Lua (5)
  • [kubelet-check] It seems like the kubelet isn‘t running or healthy.
  • 基于Python+Sqlite实现的选课系统
  • 如何查看java的字节码文件?javap?能用IDEA吗?
  • Ubuntu 下 nginx-1.24.0 源码分析 - ngx_process_options
  • 优雅地使用枚举参数,让项目Spring Boot项目更加优雅
  • 安装SSL证书的步骤,提高网站安全性与用户信任度
  • fps僵尸:12.丧尸伤害检测
  • DeepSeek 云原生分布式部署的深度实践与疑难解析—— 从零到生产级落地的全链路避坑指南
  • Matlab 移动最小二乘法(MLS,一维)
  • Java数据结构第十二期:走进二叉树的奇妙世界(一)
  • linux网络安全设置
  • 网络安全:DeepSeek已经在自动的挖掘漏洞
  • 大连无网站的企业有哪些/站长之家域名查询排行
  • 赤峰市做网站建设的公司/百度推广工作怎么样
  • c 做网站的六大对象/杭州seo网络推广
  • 双语网站建设定制开发/2345网址导航官网
  • IT周末做网站违反制度么/一个关键词要刷多久
  • 网站开发ceac证/网站流量统计分析工具