この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:6
ページ更新者:爽健
更新日時:2026-05-15 03:52:15

タイトル: ajaxでPOST通信する際の注意点
SEOタイトル: LaravelにおけるajaxでPOST通信する際の注意点

この本文には実行可能なタグ等が含まれるため、安全のためHTMLソースとして表示しています。本文データは変更していません。

<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
	<thead>
		<tr><th style="background-color:#f39c12;color:#ffffff;text-align:left">この記事の要点</th></tr>
	</thead>
	<tbody>
		<tr>
			<td style="background-color:#fef9e7">
			<ul>
				<li>Laravel で<strong>Ajax POST 通信</strong>を行うときの注意点</li>
				<li><strong>CSRF トークン必須</strong> — head タグに <code><meta name="csrf-token"></code> を仕込む</li>
				<li>Ajax 側: <code>$.ajaxSetup({headers: {'X-CSRF-TOKEN': token}})</code></li>
				<li>これが無いと <code>419 Page Expired</code></li>
			</ul>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>



<p>LaravelにおいてajaxでPOST通信する際は通常のPOST通信と同様に「<strong>csrf-token</strong>」を含める必要があります。</p>

<p>&nbsp;</p>

<p>まずLayoutのheadタグ内に「<strong>csrf-token</strong>」を含める。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff"><meta name="csrf-token" content="{{ csrf_token() }}"></span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<p>ajaxのパラメータにトークンを渡す。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff"><?php<br />
			$ajaxUrl = "/testAjax";<br />
			?></span></p>

			<p><span style="color:#ffffff"><script><br />
			function testFunc(id){<br />
			&nbsp; &nbsp;&nbsp;<br />
			&nbsp;</span><span style="color:#f1c40f"> &nbsp; var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');</span></p>

			<p><span style="color:#ffffff">&nbsp;</span><span style="color:#f1c40f"> &nbsp; </span><span style="color:#ffffff">url = <?php echo json_encode($ajaxUrl); ?>;</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; $.ajax({<br />
			&nbsp; &nbsp; &nbsp; &nbsp; url &nbsp; &nbsp; &nbsp; &nbsp; : url,<br />
			&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#f1c40f">type &nbsp; &nbsp; &nbsp; &nbsp;: 'post',</span><br />
			<span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; dataType &nbsp; &nbsp;: 'json',<br />
			&nbsp; &nbsp; &nbsp; &nbsp; data &nbsp; &nbsp; &nbsp; &nbsp;: {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#f1c40f">_token: CSRF_TOKEN,</span><br />
			<span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id &nbsp; &nbsp;: id<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			&nbsp; &nbsp; })<br />
			&nbsp; &nbsp; .done(function(response) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; console.log(response);</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; if(response['data'] != null) {</span></p>

			<p style="margin-left:40px"><span style="color:#ffffff">//処理</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; } else {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(エラー);<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			&nbsp; &nbsp; })<br />
			&nbsp; &nbsp; .fail(function(response) {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; alert('エラー');<br />
			&nbsp; &nbsp; });<br />
			}; &nbsp;<br />
			</script></span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<p>コントローラー側のサンプル。(<span style="color:#e74c3c">web.appにルーティングの設定を忘れずに</span>)</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff">public function testAjax(Request $request){<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
			&nbsp; &nbsp; $id = $request->id;</span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; //処理 例:&nbsp;</span><span style="color:#2ecc71">data</span><span style="color:#ffffff">をDBから取得を取得<br />
			&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
			&nbsp; &nbsp; return response()->json(<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [<br />
			// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'</span><span style="color:#2ecc71">data</span><span style="color:#ffffff">' => $collections<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 200,[],<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSON_UNESCAPED_UNICODE<br />
			&nbsp; &nbsp; &nbsp; &nbsp; ); &nbsp; &nbsp; &nbsp; &nbsp;<br />
			&nbsp; &nbsp; }</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>