0

I am trying to submit my contact form using ajax.I added csrf token inside a meta tag too. However I am still getting error 500.

on header

  <meta name="csrf-token" content="{{ csrf_token() }}">

my laravel route :

Route::post('/mail',[
'uses'=>'ContactController@postContact',
'as'=>'api.postContact'

]);

controller method

public function postContact(Requests $request)
{

    if(Request::ajax()) {
      $data = $request::all();
        return response()->json([
            'data'=>$data
        ]);
    }

    //$contact = new ContactModel($sub, $sender, $email, $body);
   //ignore the fact that i am not properly utilizing variables as i am testing.
}

ajax call

    $.ajaxSetup({
        header:$('meta[name="csrf-token"]').attr('content')
    });
    $.ajax({
        url:'http://abounde.com/mail',
        type:'POST',
        dataType:'json',
        data:$(this).serialize(),
        success:function(data){
            if(data){
                   console.log('submitted ');                       
            }else{ 
            //default bg
                    console.log('problem submission ');  

            }

        },
        error: function(data){
                console.log(data);
        } 
    });

error: enter image description here

Nurul Alam
  • 342
  • 3
  • 22
  • @RolfPedroErnst that is laravel built in class to return json response – Nurul Alam Aug 03 '16 at 08:45
  • Possible duplicate of [Laravel csrf token mismatch for ajax POST Request](http://stackoverflow.com/questions/32738763/laravel-csrf-token-mismatch-for-ajax-post-request) – Danh Aug 03 '16 at 08:57
  • Error 500 is usually not a sign that authentication fails, it's more a sign of some code error letting the server stall. Did you have a look the the ResponseText? – Kjell Aug 03 '16 at 09:19
  • @Kjell The responseText has html of 404 page – Nurul Alam Aug 03 '16 at 09:28
  • in your ajax error handler, append the `data.responseText` to any div in your page to see the error response. – jaysingkar Aug 03 '16 at 10:35
  • @jaysingkar as i said...i already checked the data. this is just returning 404 page – Nurul Alam Aug 03 '16 at 10:57
  • @Nurul Yes, but 404 would mean that the resource or route is not present . right ? If you check the 404 page you might also get the error stack in it. – jaysingkar Aug 03 '16 at 11:25
  • Can you post your request header? In the laravel docs the ajax header preset is written a bit differently...: https://laravel.com/docs/master/csrf#csrf-x-csrf-token – Kjell Aug 03 '16 at 11:29

1 Answers1

0

the problem is that you are not passing your token correctly. In your blade create js variable

var token = '{{Session::token()}}'; 
or 
var token = '{{csrf_token()}}';

You need to also set your route

var YOURROUTE = '{{Route("routename")}}';

Then in your app.js do something like this:

  $.ajax({
  method:"post",
  url:YOURROUTE,
  data:{
    _token:token, //this is very important
    some_data:some_data,
    next_data:next_data
  }
  }).done(function(msg) {
    console.log('Your response'+msg);
  });
Martin
  • 1,259
  • 3
  • 17
  • 36