この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:5
ページ更新者:爽健
更新日時:2019-01-07 20:46:21

タイトル: 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">&lt;meta name=&quot;csrf-token&quot; content=&quot;{{ csrf_token() }}&quot;&gt;</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">&lt;?php<br />
			$ajaxUrl = &quot;/testAjax&quot;;<br />
			?&gt;</span></p>

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

			<p><span style="color:#ffffff">&nbsp;</span><span style="color:#f1c40f"> &nbsp; </span><span style="color:#ffffff">url = &lt;?php echo json_encode($ajaxUrl); ?&gt;;</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;: &#39;post&#39;,</span><br />
			<span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; dataType &nbsp; &nbsp;: &#39;json&#39;,<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[&#39;data&#39;] != 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(&#39;エラー&#39;);<br />
			&nbsp; &nbsp; });<br />
			}; &nbsp;<br />
			&lt;/script&gt;</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-&gt;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()-&gt;json(<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [<br />
			// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#39;</span><span style="color:#2ecc71">data</span><span style="color:#ffffff">&#39; =&gt; $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>