简体   繁体   中英

My Ajax keeps returning 419 and 500 error status

I'm making a form that's called by Ajax and trying to configure the form to submit using Ajax. This form is supposed to submit the data through route('ruangrapat.store). But every time I submit the form, it returns 419 status when I don't use csrf_token() in Ajax and if I use the csrf_token() in Ajax. It always returns 500 internal server error. Can someone help me to solve this problem? I've been reading almost every discussion that I found on the internet, but still no answer.

Maybe I missed something important on my code. Please review my code.

//ajax
$(document).ready(function(){
             $('#form-ruangrapat').on('submit',function(e){

            e.preventDefault();
             var formdata=$('#form-ruangrapat').serialize();//should i do this??
//if i should take the value of inputs 1 by 1,please show me the proper way

             var token="{!!csrf_token()!!}"
            $.ajax({ 
                url:"{{route('ruangrapat.store')}}",
                data: {formData:formdata,_token:token},
                type:'post',
                success:function(result){
                    $('#result').html(result);
                }
            });
        });
        });


//controller
public function store(Request $request)
    {
        $data = new Ruangrapat();
       ...
        $data->contact = $request->get('contact');
        $data->save();
        return view('ajax-result.ruangrapat.index')->with('status', 'Ruang rapat baru berhasil ditambahkan!');
//is this return value correct??

    }

//route
Route::resource('ruangrapat', 'RuangrapatController');

我有同样的问题,419与csrf令牌匹配,当你修复它时,请求转到服务器所以,内部服务器错误500说存储时有问题,所以重新查看控制器中的存储功能和确保函数中的所有过程都正确。

First Recomendation

To make any ajax request, it is recommended to add the CSRF token to the header of the ajax requests

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

As advised by laravel documentation

Second Recomendation

To see what the ajax request is sent, I would advise having the controller return the data to you and see it through the web console.

$(document).ready(function(){
    $('#form-ruangrapat').on('submit',function(e){
        e.preventDefault();
        var formdata=$('#form-ruangrapat').serialize();
        var token="{!!csrf_token()!!}";
        $.ajax({
            url:"{{route('ruangrapat.store')}}",
            data: {formData:formdata,_token:token},
            type:'post',
            dataType:'JSON',
            //You can use sucess or done, personally i like more done
        }).done(function (data) {
            console.log(data)
        });
    });
});

And in the controller

public function store(Request $request)
{
    return response()->json($request->input());
}

Other option is to use the network monitor of the browsers and see the variables that are sent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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