我想发送一个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'); –