是什么导致此 Laravel 8 应用程序出现“419 未知状态”错误?

What causes the "419 unknown status" error in this Laravel 8 application?

我正在用 Laravel 8 和 Bootstrap 制作一个博客应用程序 5. 它有一个用户配置文件管理系统。

我想让每个用户都能添加和删除her/his头像。为此,我的 UserController 控制器中有一个 deleteavatar() 方法:

public function deleteavatar($id, $fileName)
{
  $current_user = Auth::user();
  $current_user->avatar = "default.png";
  $current_user->save();

  if (File::exists(public_path('images/avatars/' . $fileName))) {
    File::delete(public_path('images/avatars/' . $fileName));
  }
}

在路由文件中:

Route::group(['prefix' => 'user'], function() {
    Route::get('/', [UserController::class, 'index'])->name('user');
    Route::match(['get', 'post'],'/update', [UserController::class, 'update'])->name('user.update');
    Route::post('/deleteavatar/{id}/{fileName}', [UserController::class, 'deleteavatar'])->name('user.deleteavatar');
});

我用这一段JavaScript调用上面的方法:

function deleteAvatar(e) {
    e.preventDefault();

    var avatar = document.querySelector('#avatar-container img');
    var topAvatar = document.querySelector('#top_avatar');
    var trashIcon = e.currentTarget;
    var defaultAvatar = APP_URL + '/images/avatars/default.png';

    //Get user's ID
    var id = trashIcon.dataset.uid;
    var fileName = avatar.getAttribute('src').split('/').reverse()[0];

    var url = APP_URL + `/dashboard/user/deleteavatar/${id}/${fileName}`;

    if (confirm('Delete the avatar?')) {
      var CSRF_TOKEN = document.querySelectorAll('meta[name="csrf-token"]')[0].getAttribute('content');

      var xmlhttp = new XMLHttpRequest();
      xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == XMLHttpRequest.DONE) { 
          if (xmlhttp.status == 200) {
              avatar.setAttribute('src', defaultAvatar);
              topAvatar.setAttribute('src', defaultAvatar);
              trashIcon.remove();
          }
        }
      }

      xmlhttp.open('POST', url, true);
      xmlhttp.send();
    }
}
document.querySelector('#delete-avatar').addEventListener('click', deleteAvatar);

问题

由于我无法找出的原因,Chrome 控制台抛出此 419 (unknown status)

我的错误是什么?

Laravel 由于缺少 csrf 令牌而返回 http 状态 419。在 HTML meta 标签中添加 csrf 令牌,并添加到您的 ajax 请求中 header。

供参考:https://laravel.com/docs/8.x/csrf#csrf-x-csrf-token

为 csrf 令牌设置 xmlhttp header

function deleteAvatar(e) {
    e.preventDefault();

    var avatar = document.querySelector('#avatar-container img');
    var topAvatar = document.querySelector('#top_avatar');
    var trashIcon = e.currentTarget;
    var defaultAvatar = APP_URL + '/images/avatars/default.png';

    //Get user's ID
    var id = trashIcon.dataset.uid;
    var fileName = avatar.getAttribute('src').split('/').reverse()[0];

    var url = APP_URL + `/dashboard/user/deleteavatar/${id}/${fileName}`;

    if (confirm('Delete the avatar?')) {
        var CSRF_TOKEN = document.querySelectorAll('meta[name="csrf-token"]')[0].getAttribute('content');

        var xmlhttp = new XMLHttpRequest();

        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == XMLHttpRequest.DONE) { 
                if (xmlhttp.status == 200) {
                        avatar.setAttribute('src', defaultAvatar);
                        topAvatar.setAttribute('src', defaultAvatar);
                        trashIcon.remove();
                }
            }
        }

        xmlhttp.open('POST', url, true);
        xmlhttp.setRequestHeader("X-CSRF-TOKEN", CSRF_TOKEN);
        xmlhttp.send();
    }
}
document.querySelector('#delete-avatar').addEventListener('click', deleteAvatar);

参考:https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader

参考:https://laravel.com/docs/9.x/csrf