0

自从我开始在 Windows 10 上使用 APACHE 作为开发环境以来,我一直面临这个问题。绝对有很多链接和帖子讨论相同的问题,这些链接都没有解决这个问题。

我收到“419 未知状态”的请求以及一堆其他跟踪详细信息:

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

当我检查 laravel.log 它说,

local.ERROR: The MAC is invalid.

A. 为了清楚起见,我在刀片 / html 页面中有 CSRF 令牌,我在 AJAX 调用中将其作为标题传递:

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

B. 我试过 - 清除浏览器 cookie,然后清除 artisan

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

C. 我尝试使用 composer dump-autoload 进行清理,然后使用 clear-cache

composer clear-cache
composer dump-autoload

对于专家的细读,这就是我的 ajax 调用/Laravel 控制器/路由代码的样子:

AJAX 调用:

在下面的代码中,param_cust_unique_id 作为参数传递给调用此 ajax 调用的函数。

$.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::post('getBalanceForCustomer', 'CustomersController@getBalanceForCustomer');

Laravel 控制器方法:

public function getBalanceForCustomer(Request $request) {

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

我正在使用开发环境:Visual Studio Code、MySQL Workbench、Chrome 浏览器。

技术栈是:Jquery、AngularJS、Laravel 5.5、MariaDB

非常感谢这方面的任何帮助。

4

1 回答 1

2

在标题中使用此元标记

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

然后ajax调用:

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);
         }
  });
于 2019-04-14T07:27:16.707 回答