Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging jQuery AJAX response: what I'm doing wrong?

$.ajax({
    type: 'POST',
    url: 'place/add',
    data: {
        lat: lat,
        lng: lng,
        name: name,
        address: address,
        phone: phone,
        review: review,
        category: category
    },
    success: function(data) {
    alert(data);
    alert(data.id);
    // ......
});

The first alert gives:{"id":"2","success":true}, but the second: undefined

like image 900
Derk Avatar asked May 29 '26 11:05

Derk


1 Answers

You need to specify your anticipated returned data type as JSON:

$.ajax({
    type: 'POST',
    dataType: 'json', // specifies the return type
    url: 'place/add',
    data: {
        lat: lat,
        lng: lng,
        name: name,
        address: address,
        phone: phone,
        review: review,
        category: category
    },
    success: function(data) {
        alert(data);
        alert(data.id);
        // ......
    }
});
like image 158
Corey Ballou Avatar answered Jun 01 '26 00:06

Corey Ballou



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!