bramsky
bramsky

Reputation: 11

Alert not showing when I do a passed response

$bd = Carbon\Carbon::now()->diffInYears(Carbon\Carbon::parse(request()->input('form.birthdate');

if ($bd <= 6){          
                return response(['message' =>  "That's less than 6, not allowed",500]);
}

and in my axios request

}).then(response => {
                    if (response.status === 500 ){
                        alert(error.response.data.message);
                    }
                    else { 
                    window.location.replace("/admin/users/"+this.user.id);
                }

I don't know why my alert not showing there. iT sucks?

Upvotes: 0

Views: 136

Answers (3)

Akhzar Javed
Akhzar Javed

Reputation: 628

How i handle axios is below First return your response from laravel like this. (Include the status code)

return response(['message' =>  "That's less than 6, not allowed"], 400);

After that on your axios side get the message like this

axios.post().then(function(response){
                // this will run if the status code is 200 in laravel response
               // Get passed data like 'response.data.message', You will get your passed values after 'response.data'
            }).catch(function(error){
                notify('error', error.response.data.message); // You will get your passed values after 'error.response.data'
            })

The catch function will run when your ajax response is 400 500 etc etc

Upvotes: 1

Arnaud
Arnaud

Reputation: 41

Parmeters for the response helper are response(string message, int status_code, array? headers)

If you wan't return json use return response()->json(array json, int status_code, array? headers)

For more informations see the documentation https://laravel.com/docs/7.x/helpers#method-response

Upvotes: 0

Christophe Hubert
Christophe Hubert

Reputation: 2951

Modify your response as follows:

return response(['message' =>  "That's less than 6, not allowed"], 500);

Upvotes: 0

Related Questions