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');
}
You don’t parse the response.
datais still a string.Set
dataType: 'json'in the$.ajaxoptions.Even better if you set the right content type for the response in PHP: