laravel ajax 后序列化不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44616979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:10:02  来源:igfitidea点击:

laravel ajax post serialize not working

phpjqueryajaxlaravel

提问by vincent villafuerte

                    <form id="sendmemessage">
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                            <div class="form-group">
                                <h2 class="open-f bbold green-text contact-t">SEND ME A MESSAGE!</h2>
                            </div>
                            <div class="form-group">
                                <input type="text" name="name" class="form-control input-sm no-radius" aria-describedby="emailHelp" placeholder="Name">
                            </div>
                            <div class="form-group">
                                <input type="text" name="email" class="form-control input-sm no-radius" placeholder="Email">
                            </div>
                            <div class="form-group">
                                <input type="text" name="subject" class="form-control input-sm no-radius" placeholder="Subject">
                            </div>
                            <div class="form-group">
                                <textarea class="form-control input-sm no-radius" name="message" rows="5" placeholder="Message"></textarea>
                            </div>
                            <div class="form-group">
                                <button type="submit" class="btn btn-custom pull-right btn-w">Send Message</button>
                            </div>
                            <br>
                    </form>

I am trying to subit the values of the form,i know this is just basic but i dont know why is not working.

我正在尝试提交表单的值,我知道这只是基本的,但我不知道为什么不起作用。

below is my ajax,

下面是我的ajax,

$("#sendmemessage").submit(function(stay){
    $.ajax({
        type: 'POST',
        url: "{{ url('/') }}/message_me",
        data: $(this).serialize(),
        success: function (data) {
           alert();
        },
    });
    stay.preventDefault(); 
});

my route

我的路线

Route::post('message_me','home_controller@message_me');

my controller

我的控制器

class home_controller extends Controller{

    public function message_me(){

        echo "its here!";

    }

}

here are my form details code

这是我的表单详细信息代码

回答by Riaz Laskar

$(this).serialize()was inside the ajax object and it refers to ajax not the form.

$(this).serialize()位于 ajax 对象内,它指的是 ajax 而不是表单。

$("#sendmemessage").submit(function(stay){
   var formdata = $(this).serialize(); // here $(this) refere to the form its submitting
    $.ajax({
        type: 'POST',
        url: "{{ url('/') }}/message_me",
        data: formdata, // here $(this) refers to the ajax object not form
        success: function (data) {
           alert();
        },
    });
    stay.preventDefault(); 
});

回答by Riaz Laskar

Start with replacing

从更换开始

<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">

with the helper function

带辅助功能

{!! csrf_field() !!}

I structure my ajax calls like the following:

我的ajax调用结构如下:

$("#sendmemessage").on('submit', function(e) {
    e.preventDefault();
    var data = $("#sendmessage").serialize();
    $.ajax({
        type: "post",
        url: "/message_me",
        data: data,
        dataType: "json",
        success: function(data) {
            console.log('success');
        },
        error: function(error) {
            console.log('error');
        }
    });
});

Can you access the "message_me" route via the browser? A 500 internal server error should give you clear insight why the request fails.

您可以通过浏览器访问“message_me”路由吗?500 内部服务器错误应该让您清楚地了解请求失败的原因。