简体   繁体   English

如何在Laravel中使用Vue.js发送表单?

[英]How to send form using Vue.js in laravel?

I am trying to build form and send it to laravel backend using post vue. 我正在尝试构建表单,并使用post vue将其发送到laravel后端。 But that's not working. 但这不起作用。 What can I do to improve it, and how to put the csrf field? 我可以做些什么来改进它,以及如何放置csrf字段?

Form: 形成:

<div class="modal fade" id="modal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Search people!</h4>
            </div>
            <div class="modal-body">
                <form method="POST" class="">
                    <div class="form-group">
                        <label for="city">City:</label>
                        <input type="text" class="form-control" v-model="football.city" id="city">
                    </div>
                    <button v-on:click.prevent="getFilterRequest()" class="btn btn-blue">Search</button>
                </form>
            </div>
        </div>
    </div>
</div>

Vue: VUE:

<script>
    export default {
        props: [
            'user',
            'users'
        ],
        data: function () {
          return {
            usr: [],
            football: [], 
            }
        },
        mounted() {

        },
        methods: {
          getAvatarUrl()
          {
            return 'img/lock_thumb.jpg';
          },
          getFilterRequest()
          {
            return this.$http.post('/football/search', new FormData(this.$refs.myForm));
          }
        }
    }
</script>

And error: 错误:

Uncaught TypeError: Cannot read property 'post' of undefined 未捕获的TypeError:无法读取未定义的属性“ post”

in vue 2 with laravel 5, laravel comes with presets for this using axios. 在laravel 5的vue 2中,laravel使用axios附带了为此的预设。

js JS

import axios from 'axios'

//vue data
data : {
  football.city: ''
}


methods: {
    getFilterRequest() {
        axios['post']('/football/search', this.data)
            .then(response => console.log(response))
            .catch(error => console.log(error));
    }
}

I don't use vue-resource, but the principal's the same. 我不使用vue-resource,但是主体是相同的。 You need to put your form in your post. 您需要将表格放入您的帖子中。 Give your form a ref, then create a FormData object, like this... 给您的表单一个引用,然后创建一个FormData对象,像这样...

markup 标记

<form method="POST" class="" ref="myForm">

js JS

return this.$http.post('/football/search', new FormData(this.$refs.myForm))

Don't forget to import vue-resource, like the man says :-) 不要忘了导入vue资源,就像那个男人说的:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM