Dom
Dom

Reputation: 878

Laravel 500 (Internal Server Error) on Ajax Post

I've tried doing my research on my issue but have not been able to solve it. I'm attempting to Ajax POST on click. I've read the most popular issue is due to the csrf_token, but I believe I have that handled properly?

I keep getting this error:

POST http://example.com/refreshCalendar 500 (Internal Server Error)

Here is my code...

My meta tag for the csrf token at the top of my master.blade.php file

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

Route:

Route::post('/refreshCalendar', ['as' => 'refreshCalendar', 'uses' =>'Calendar@refreshCalendar']);

Js function

function refreshCalendar(obj){
var month = obj.data('month');
var year = obj.data('year');
history.pushState(null, null, '/month/'+month+'/year/'+year);

var data = {
    "month":month,
    "year":year,
    _token:$('meta[name="csrf-token"]').attr('content')
 };

$.ajax({
     type: "POST",
     url: '/refreshCalendar',
     dataType: 'html', 
     async:true,
     data: data,
     success: function(data){
             $('#calendarHolder').html(data);

     },
     error: function(){alert("There was an error retrieving information");return false;}
 });

}

My Controller:

namespace App\Http\Controllers;

use DateTime;
use Illuminate\Http\Request;  

class Calendar extends Controller
{


public function refreshCalendar(Request $request)
    {
        //Set data to $request
        $data = $request->all();
        return show($data['month'], $data['year'], true);
    }

}

Upvotes: 0

Views: 1304

Answers (2)

Alena Lel&#39;
Alena Lel&#39;

Reputation: 1

If meta is present, you should look to your network for issues.

That was the culprit for me:

such as here

Upvotes: 0

Serdar Saygılı
Serdar Saygılı

Reputation: 471

<meta name="token" content="{{ csrf_token() }}">
_token:$('meta[name="csrf-token"]').attr('content')

Your meta tag name is token, however you are looking for the meta tag named csrf-token.

Upvotes: 1

Related Questions