Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a JSON response

I get the following response from the server after doing an ajax request:

{"error":false,"success":true}

My ajax code:

$.ajax({
    url: '/update',
    type: 'post',
    data: $(this).serialize(),
    success: function(response) {
        alert(response)
    },
    error: function() {
        alert('An error occured, form not submitted.');
    }
});

instead of alerting the whole response I want to alert the value of "success", which in this case would be true. How to do this?

like image 671
Linda Avatar asked Aug 02 '26 14:08

Linda


2 Answers

Like so:

alert(response.success);
like image 150
Naftali Avatar answered Aug 04 '26 03:08

Naftali


   $.ajax({
        url: '/update',
        type: 'post',
        dataType: 'json', 
        data: $(this).serialize(),
        success: function(response) {

                        alert(response.success)

        },
        error: function() {
            alert('An error occured, form not submitted.');
        }
    });
like image 26
genesis Avatar answered Aug 04 '26 03:08

genesis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!