Robbie
Robbie

Reputation: 63

data.success undefined

Working on a submission form with ajax, json and PHP. The data is handled properly with the DB, but this script, with alert(data.success), says data.success is undefined. If I alert(data), it shows what i need is there {"success":"http:\/\/myaddress.com"}

function confirmSubmit() {
$.ajax({
    type: 'POST',
    url: 'index.php?route=payment/authorize/send',
    data: $('#authorize :input'),   
    beforeSend: function() {
        var img = '<?php echo $text_wait; ?>';
        $('#authorize_button').attr('disabled', 'disabled');

        $('#authorize').before('<div class="wait"><img src="catalog/view/theme/default/image/loading_1.gif" alt="" /> ' + img + '</div>');
        alert('Start');
    },
    success: function(data) {
        if (data.error) {
            alert('errors...');
            alert(data.error);

            $('#authorize_button').attr('disabled', '');
        }

        $('.wait').remove();

        if (data.success) {
            alert('success! It should redirect.');
            location = data.success;
        }
        else {
            alert('it worked... but won\'t redirect...');
            alert(data.success);
        }

    }
});
alert('End');

}

Upvotes: 0

Views: 2799

Answers (2)

SeanCannon
SeanCannon

Reputation: 77976

Try adding dataType:'json' or else parse your data response as json with jQuery.parseJson(data)

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816492

You don't parse the response. data is still a string.

Set dataType: 'json' in the $.ajax options.

Even better if you set the right content type for the response in PHP:

header('Content-type:  application/json.');

Upvotes: 5

Related Questions