Reputation: 2505
Here the scenario is, I want to fetch some data from database and show it to user. While someone give some input and save it to database and I put a status with that which is always "0
" until someone see it. So whenever a input has submitted user will see a pop up screen with that new data has inserted in another page. Here the problem is when ajax got some value with status "0
" it shown it to screen but when it couldn't fetch any value ( when all the status value is 1
)it shows error in console :
TokenMismatchException in VerifyCsrfToken.php line 67
.
How do I solve the issue , any possible suggestion please ?
Here is the route:
Route::post('/unread',[
'uses'=>'ItemController@getUnread',
'as'=>'unread'
]);
Route::post('/s_update',[
'uses'=>'ItemController@status_update',
'as'=>'s_update'
]);
Here is the controller :
public function getUnread()
{
$items=DB::select(DB::raw("SELECT count(*) as total from items where status =0"));
// $data=mysql_fetch_assoc($items);
return Response::json($items);
}
public function status_update()
{
$items=DB::select(DB::raw("UPDATE items SET status=1 WHERE status=0"));
return Response::json($items);
}
and here is the ajax call :
<script type="text/javascript">
setInterval(function(){
$(".container").load("list", function(){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type : "POST",
url : "{{url('unread')}}",
dataType : 'json',
success : function(data) {
$.each(data,function(index,subcatObj){
if(subcatObj.total != '0')
{
var yes = confirm('You have '+subcatObj.total+' new messages');
if(yes){
status_update();
}
}
});
},
});
});
}, 3000);
function status_update(){
$.ajax({
url:"{{url('s_update')}}",
method:"POST",
});
}
</script>
Upvotes: 0
Views: 1765
Reputation: 10651
Change this route
Route::post('/unread',[
'uses'=>'ItemController@getUnread',
'as'=>'unread'
]);
to
Route::get('/unread',[
'uses'=>'ItemController@getUnread',
'as'=>'unread'
]);
Upvotes: 1
Reputation: 1593
I think this is because you are not sending the laravel csrf token in the post.
You need to send a field called "_token" in the json which is the csrf_token provided.
See here : https://laravel.com/docs/5.3/csrf
Upvotes: 0