0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravel 10, Ajax通信で419エラー, X-CSRF-TOKENは入れてある

Posted at

環境

PHP 8.3.15
Laravel Framework 10.28.0

どこ見ても X-CSRF-TOKEN 入れろって書いてある

もうやってあるんよ・・
どうやってもこれしかヒットしなくて困った。

$.ajax({
    // どこ見ても ↓ これ ↓入れろって書いてあるけどすでに入れてある
    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
	url: 'example',
	type: "post",
	dataType: "json",
		data: {
			key1: val
			key2: val
		},
	}).then(

解決した方法

data の方に _token というキーを追加してやったらうまくいったのでした。

$.ajax({
	// headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
	url: 'example',
	type: "post",
	dataType: "json",
		data: {
			key1: val,
			key2: val,
            // ↓ これを追加
            _token: $('meta[name="csrf-token"]').attr('content'),
		},
	}).then(
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?