Reputation: 466
I want to redirect using return data in JSON but it's now working. It's easy to do it in JavaScript using location.href but in Laravel Controller nothing happens. I have something like this.
if(count($modelData) > 0) {
$status = 'info';
$message = 'Logged Out Successfully';
} else {
$status = 'error';
$message = 'User Not Found';
}
$returnData = array(
'status' => $status,
'message' => $message,
'redirect' => '/visitor-logs'
);
return response()->json($returnData);
Upvotes: 1
Views: 2011
Reputation: 1374
When you want to return something like json from a certain request, that's what you get, just a json response. It doesn't make much sense that you generate a re-direct from the controller script itself.
So, whatever re-direction action hapenning, will be happening on the client side in this case. This means on the code executing on the browser and not in the server.
With that json response, in the client, with javascript you can generate a redirection with location.href
using the value from response.redirect
.
Note: As a Note I must clarify that you can generate redirects on the server side, of course. But it's not the case here.
Upvotes: 1