galih56
128
7
Laravel

My ajax keep returning 419 and 500 error status

Posted 3 weeks ago by galih56

I'm making form that's called by ajax and trying to make the form can submit using ajax.This form is supposed to submit the data through route('ruangrapat.store).But everytime i submit the form.It always return 419 status when i don't use csrf_token() in ajax and if i use the csrf_token() in ajax.It always return 500 internal error.Can someone help me to solve this problem.I've been reading almost every discussion that i found in 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');

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.