I am A PHP developer and currently moving towards Laravel framework as per my task I have to complete the realtime table using ajax but I am sticking with an error which is CSRF token mismatch error please help me to resolve the error I am posting shortcode only
JAVA Script
<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 Path
Route::post('/getMsg','CustomerSearchController@doAjaxTest');
Controller code
public function doAjaxTest(){
$msg = "<b>Message over ajax This test is Successful</b>.";
return response()->json(array('msg'=> $msg), 200);
}
HTML CODE
<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>
I don't know why it is showing me CSRF token mismatch when headers contain tokens once solved i can have some realtime action please help out
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire