CK BizTech
CK BizTech

Reputation: 23

Laravel 5.5 No Message exception on AJAX Post Call. Line 203, Handler.php, 419 unknown status

I have been facing this issue since I started using APACHE on Windows 10 as development environment. There are absolutely many links and post discussing about same issue and none of these links address this problem.

I get "419 unknown status" of request with with bunch of other trace details:

"message": "",
"exception": "Symfony\\Component\\HttpKernel\\Exception\\HttpException",
"line": 203,

When I checked with laravel.log it says,

local.ERROR: The MAC is invalid.

A. Just to get clarity, I have CSRF token in blade / html page, I have passed it in AJAX call as header:

headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') },

B. I tried with - browser cookies cleared and then, clearing artisan

php artisan cache:clear
php artisan config:clear
php artisan view:clear
php artisan route:clear

C. I have tried to clean using composer dump-autoload and then clear-cache

composer clear-cache
composer dump-autoload

For perusal of experts, this is how my ajax call / Laravel Controller / Route code looks like:

AJAX Call:

In below code param_cust_unique_id is passed as parameter to a function in which this ajax call is invoked.

$.ajax({
        type: 'post',
        headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
        },
        url: '/getBalanceForCustomer',
        data: { "cust_unique_id" : param_cust_unique_id },
        success: function (response) {
             console.log(response);
        },
        error: function (response) {
                console.log(response);
        }
    }); 

Laravel Route:

Route::post('getBalanceForCustomer', 'CustomersController@getBalanceForCustomer');

Laravel Controller Method:

public function getBalanceForCustomer(Request $request) {

//Balance calculation logic, bit longer hence cutting-it short
return $balance;
} 

I am using development environment : Visual Studio Code, MySQL Workbench, Chrome Browser.

Tech Stack is : Jquery, AngularJS, Laravel 5.5, MariaDB

Any help in this regard is highly appreciated.

Upvotes: 0

Views: 326

Answers (1)

Ubaid Ur Rehman
Ubaid Ur Rehman

Reputation: 119

use this meta tag in your header

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

and then ajax call:

let formData = $('form').serializeArray();
$.ajax({
      url: "/",
      type: "POST",
      data: {formData, "_token": $('#token').val()},
      cache: false,
      datatype: 'JSON',
      processData: false,
      success: function (response) {
           console.log(response);
         },
         error: function (response) {
           console.log(response);
         }
  });

Upvotes: 2

Related Questions