我想發送一個php數組到ajax,但它不起作用。說實話,我不知道我做錯了什麼。json_encode返回空php數組
我正在使用json_encode(),它返回null。
我的PHP代碼:
$info = array();
$info['NEW YORK CITY'] = array(
'Name' => 'New York City'
);
$city = $_POST['city'];
if (strtoupper($city) === 'NEW YORK CITY') {
echo "Name: " . json_encode($info['NEW YORK CITY']['Name']) . ".<br>";
} else {
echo "error.";
}
我的Ajax代碼:
$('form.ajax').on('submit', function() {
var that = $(this),
url = that.attr('action'),
type = that.attr('method'),
data = {};
that.find('[name]').each(function(index, value) {
var that = $(this),
name = that.attr('name'),
value = that.val();
data[name] = value;
});
//console.log(data);
$.ajax({
url: url,
type: type,
data: data,
success: function(response) {
//console.log(response);
$('form.ajax').html(response);
}
}).fail(function(jqXHR) {
alert(jqXHR.statusText);
});
return false;
});
定了!我有數組之前的json_encode。它現在起作用,我把數組放在最上面。
我可以看到'print_r($ info);'? – aldrin27
$ info = array(); $ info ['紐約市'] = array('Name'=>'紐約市','Rank'=>'1st,US','Population'=>'8,991,079'); –