如何将JSON数组{{this.row}}传递给commit()函数,以使用Axios.post进行发布

问题描述 投票:0回答:1

this.row正在将表单输入生成到JSON数组中,我正尝试使用Axios通过Submit函数将其发布,但无法提供该值,请帮助出什么问题?

Axios邮政编码

axios.post('/submit', this.rows).then(response => {
                        this.rows; //Clear input fields.
                        this.loaded = true;

这是我的完整代码

    <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>
arrays json vuejs2 laravel-5.8
1个回答
0
投票

您可以使用JSON.stringify(array_to_convert_in_string_to_send_in_ajax),但您还必须在后端服务器中对其进行json_decode解码

例如在服务器部分中的laravel:

$result = json_decode($request->your_array);
© www.soinside.com 2019 - 2024. All rights reserved.