2
In an application in Laravel I have several scripts using ajax. Everyone expects a response from the controller script and, if it returns true, I run a certain function with the 'Success' ajax
Example of how I’m doing:
script js.
$.ajax({
type: 'GET',
dataType: 'json',
url: '/teste',
data: {value:value},
success: function(data){
//Alguma função
}
});
Testecontroller.php
public function teste(Request $request){
$value = $request->value;
/* Alguma função com o value*/
return response()->json(['success' => true]);
}
But I need that, in case of error, return false. Only I have no idea and how to test this, I wanted to add a test of errors to all scripts(and I don’t even know if it’s correct or necessary ) in several cases (query database, working with session etc). I know the Try/catch but never used and hardly see in some code someone using, so I’m kind of lost with this.
And what do I return from the controller? If I do not return a Success-true then the 'Success' method of ajax does not run.
– Kelvym Miranda
In the link examples, you are teaching to capture errors in Ajax. When you return
422 status
, you have to adderror:
in your $.ajax– Wallace Maxters