我是一名 PHP 开发人员,目前正在根据我的任务转向 Laravel 框架我必须使用 ajax 完成实时表,但我坚持一个错误,即 CSRF token 不匹配错误请帮助我解决错误我只发布短代码
JAVA脚本
<script>
function getMessage() {
$.ajax({
//var data = {"_token": $('#token').val()},
type:'POST',
url:'/getMsg',
headers: {'XSRF-TOKEN': $('meta[name="_token"]').attr('content')},
success:function(data) {
$("#msg").html(data.msg);
}
});
}
</script>
路线路径Route::post('/getMsg','CustomerSearchController@doAjaxTest');
Controller 代码 public function doAjaxTest(){
$msg = "<b>Message over ajax This test is Successful</b>.";
return response()->json(array('msg'=> $msg), 200);
}
HTML 代码 <center>
<input type = "hidden" name = "_token" value = '<?php echo csrf_token(); ?>'>
<table>
<tr>
<td><label>Enter Place Name</label></td>
<td><input type="text" class="form-control" id="placename" name="placename" placeholder="Name Of Place"/></td>
</tr>
<tr>
<td>
<input type="submit" value="Get Message" onclick="getMessage()" />
</td>
</tr>
</table>
<br>
<!--
<div class="panel panel-default table-responsive">
<div id="dataTag"><b>All the Details according to department will be displayed</b></div>
</div>
-->
<div id = 'msg'>This message will be replaced using Ajax.
Click the button to replace the message.</div>
</center>
我不知道为什么当 header 包含 token 时它会向我显示 CSRF token 不匹配一旦解决,我可以进行一些实时操作,请帮忙
最佳答案
我有同样的问题。我改了 APP_NAME 在 .env 文件中设置为默认值(Laravel)。尝试一下。
关于php - 消息 : "CSRF token mismatch.", 异常 : "Symfony\Component\HttpKernel\Exception\HttpException", …},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62673918/