我有以下代码返回一个 JSON 数组。我在将值放入 HTML 输入时遇到问题:
$('#button-validate').live('click', function() {
$.ajax({
url: 'index.php?route=sale/customer/addressValidation&token=<?php echo $token; ?>',
type: 'post',
dataType: 'html',
data: 'shipping_address_1=' + encodeURIComponent($('input[name=\'address[1][address_1]\']').val()) + '&shipping_address_2=' + encodeURIComponent($('input[name=\'address[1][address_2]\']').val()) + '&shipping_city=' + encodeURIComponent($('input[name=\'address[1][city]\']').val()) + '&shipping_region=' + encodeURIComponent($('select[name=\'address[1][zone_id]\']').val()) + '&shipping_zip=' + encodeURIComponent($('input[name=\'address[1][postcode]\']').val()),
beforeSend: function() {
$('.success, .warning').remove();
$('#button-history').attr('disabled', true);
$('#history').prepend('<div class="attention"><img src="view/image/loading.gif" alt="" /> Validating</div>');
},
complete: function() {
$('#button-history').attr('disabled', false);
$('.attention').remove();
},
success: function(data) {
if (data['error']) {
$('div#warning').attr('display', false);
$('div#warning').after('<div class="warning" style="display: none;">' + json['error'] + '</div>');
}else{
$('#shipping_address_1').val(html['shipping_address_1']);
$('#shipping_address_2').val(html['shipping_address_2']);
$('#shipping_city').val(html['shipping_city']);
$('#shipping_zip').val(html['shipping_zip']);
}
}
});
});
JSON 数组在 Firebug 中如下所示:
[{"shipping_address_1":"21497 CROZIER AVE","shipping_address_2":"","shipping_city":"BOCA RATON","shipping_region":"FL","shipping_zip":"33428"}]