Arrays 如何将JSON数组{{this.row}}传递给submit()函数以使用Axios.post发布
this.row正在将表单输入生成JSON数组,我正试图使用Axios通过提交函数发布该数组,但无法获取该值。请帮助,出了什么问题 Axios邮政编码 这是我的完整代码Arrays 如何将JSON数组{{this.row}}传递给submit()函数以使用Axios.post发布,arrays,json,vuejs2,laravel-5.8,Arrays,Json,Vuejs2,Laravel 5.8,this.row正在将表单输入生成JSON数组,我正试图使用Axios通过提交函数发布该数组,但无法获取该值。请帮助,出了什么问题 Axios邮政编码 这是我的完整代码 <template> <form @submit.prevent="submit"> {{ /* These are 3 buttons which are calling 3 different function to create input boxes */ }}
<template>
<form @submit.prevent="submit">
{{ /* These are 3 buttons which are calling 3 different function to create input boxes */ }}
<div class="d-flex mt-5"><div>
<label>Add A</label>
<button type="button" @click="addRow">01</button>
</div>
<div> <label>Add B</label>
<button type="button" @click="addRow1">02</button>
</div>
<div> <label>Add c</label>
<button type="button" @click="addRow3">03</button>
</div>
</div>
{{ /* this section calls button-counter template from script code */ }}
<div v-for="row in rows" :key="row.id">
<button-counter :name ="row.name" :id="row.id" :value.sync="row.value"></button-counter>
</div>
<div>
<button type="submit" class="btn btn-primary">Add Points</button>
</div>
<div v-if="success" class="alert alert-success mt-3">
Message sent!
</div>
</form>
</template>
<script>
Vue.component("button-counter", {
props: {
value: {
default: "",
}
},
/* This is my template which gets called fro the addition of new inputs ...guess here I need to add v-model so that dynamically generated fields will be posted but I'm unable to get it posted */
template: '<input class="form-control" id= id.row name=row.name type="number" style="margin-top: 10px;" :value="value" @change="$emit(\'update:value\', $event.target.value)">'
});
export default {
props: ['gameId','userId'],
mounted() {
console.log('Component mounted.')
},
data() {
return {
gamex: this.gameId,
rows: [],
count: 0,
fields: {},
errors: {},
success: false,
loaded: true,
};
},
computed: {
total() {
if (this.rows.length) {
return this.rows.reduce((acc, row) => acc += parseInt(row.value), 0);
}
return 0;
}
},
methods: {
addRow: function() {
var txtCount = 1;
let id = "txt_" + txtCount;
this.rows.push({ name:'zero',value:100, description: "textbox1", id });
},
addRow1: function() {
var txtCount = 1;
let id = "txt2_" + txtCount;
this.rows.push({name:'one',value:200, description: "textbox2", id });
},
addRow3: function() {
var txtCount = 1;
let id = "txt3_" + txtCount;
this.rows.push({name:'two',value:300, description: "textbox3", id });
},
submit: function() {
if (this.loaded) {
this.loaded = false;
this.success = false;
this.errors = {};
axios.post('/submit', this.rows).then(response => {
this.rows; //Clear input fields.
this.loaded = true;
this.success = true;
}).catch(error => {
this.loaded = true;
if (error.response.status === 422) {
this.errors = error.response.data.errors || {};
}
});
}
},
followUser() {
axios.post('/chklet/' + this.userId)
.then(response => {
return response.data ;
});
}
},
mounted () {
this.followUser();
}
};
</script>
{{/*这是3个按钮,它们调用3个不同的函数来创建输入框*/}
添加
01
加B
02
加c
03
{{/*此部分从脚本代码*/}调用按钮计数器模板
加分
消息已发送!
Vue.组件(“按钮计数器”{
道具:{
价值:{
默认值:“”,
}
},
/*这是我的模板,在添加新输入时被调用…我想这里我需要添加v-model,以便动态生成的字段将被发布,但我无法发布它*/
模板:“”
});
导出默认值{
道具:['gameId','userId'],
安装的(){
console.log('组件已安装')
},
数据(){
返回{
gamex:this.gameId,
行:[],
计数:0,
字段:{},
错误:{},
成功:错,
是的,
};
},
计算:{
总数(){
if(this.rows.length){
返回this.rows.reduce((acc,row)=>acc+=parseInt(row.value),0);
}
返回0;
}
},
方法:{
addRow:function(){
var txtCount=1;
让id=“txt_216;”+txtCount;
this.rows.push({name:'zero',value:100,description:'textbox1',id});
},
addRow1:函数(){
var txtCount=1;
让id=“txt2”+txtCount;
this.rows.push({name:'one',value:200,description:“textbox2”,id});
},
addRow3:函数(){
var txtCount=1;
让id=“txt3”+txtCount;
this.rows.push({name:'two',value:300,description:“textbox3”,id});
},
提交:函数(){
如果(已加载此文件){
this.loaded=false;
成功=错误;
this.errors={};
post('/submit',this.rows)。然后(response=>{
this.rows;//清除输入字段。
this.loaded=true;
这就是成功;
}).catch(错误=>{
this.loaded=true;
if(error.response.status==422){
this.errors=error.response.data.errors{};
}
});
}
},
followUser(){
axios.post('/chklet/'+this.userId)
。然后(响应=>{
返回响应数据;
});
}
},
挂载(){
this.followUser();
}
};
您可以使用JSON.stringify(数组\u-to\u-convert\u-in\u-string\u-to\u-send\u-in\u-in\u-ajax)
但您还必须在后端服务器中对其进行JSON解码
在服务器部分,例如laravel:
$result = json_decode($request->your_array);
您可以使用
JSON.stringify(数组\u-to\u-convert\u-in\u-string\u-to\u-send\u-in\u-ajax)
但您还必须在后端服务器中对其进行JSON解码
$result = json_decode($request->your_array);