When I make an AJAX request to my webserver, sometimes I get an error.
But the error is embedded in a whole load of CSS and HTML and it's very hard to see what the error message actually is!
Is there some setting that removes all this styling?
Hello.
Normally Laravel return a JSON response for AJAX requests (but the client must specify it). To specify it from the client, just add the X-Requested-With header with the value XMLHttpRequest.
Something like this:
$.ajax
({
type:"POST",
beforeSend: function (request)
{
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
},
...
...
If you want to return a JSON or plain response all the time, just catch in your own way the Exception in the Exceptions/Handler.php file.
Something like this in the render method:
if($e instanceof NotFoundHttpException)
{
return response()->json(['message' => 'Bad address', 'code' => 400], 400);
}
You can get detailed info and step by step course about this here: RESTful API with Laravel
Or something more general here: Laravel Course, step-by-step
Hope it helps. Best wishes.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community