我试图从ajax中回复一个json响应,在这里我要求数据以及我在响应时所做的操作:
$.ajax({
type: "post",
dataType : "json",
url: ea_map_ajaxurl,
data: {
action: 'get_markers_info',
id : marker.id,
ids_prodotti: marker.ids_prodotti,
ids_owner: marker.ids_owner
},
success: function(data){
response = JSON.parse(data);
$(response).each(function(){
console.log(this.id);
});
}
});
这是json的回复,我已经使用jsonlint测试了它的有效性,但我无法管理。
[
{
"id": "938",
"post_title": "Cooperativa Arvaia",
"indirizzo": "n.p.",
"term_taxonomy_id": [
"117",
"190"
],
"latitudine": "45",
"longitudine": "11"
},
{
"id": "1226",
"post_title": "Prova inserimento",
"indirizzo": "Via le mani dal naso, 1",
"term_taxonomy_id": [
"184"
],
"latitudine": "44.634",
"longitudine": "11"
},
{
"id": "1257",
"post_title": "Osteria Rovescio",
"indirizzo": "Via Pietralata 41",
"term_taxonomy_id": [
"109"
],
"latitudine": "45.123",
"longitudine": "10.732"
}
]
这是我创建json的函数:
function getMarkersInfo() {
global $wpdb;
$id=$_POST['id'];
$ids_prodotti=$_POST['ids_prodotti'];
$ids_owner=$_POST['ids_owner'];
$q=ea_db_get_marker_info($id,$ids_prodotti,$ids_owner);
$infos=$wpdb->get_results($q, ARRAY_A);
$json_info=Array();
foreach ($infos as $info){
if(! isset($json_info[$info['ID']])) {
$json_info[$info['ID']]=Array(
'id' => $info['ID'],
'post_title' => $info['post_title']
);
}
if($info['meta_key'] == 'ea_desc_indirizzo' &&
!isset($json_info[$info['ID']]['indirizzo'])){
$json_info[$info['ID']]['indirizzo']=$info['meta_value'];
} elseif ($info['meta_key'] == 'ea_map_latitudine' &&
!isset($json_info[$info['ID']]['latitudine'])) {
$json_info[$info['ID']]['latitudine']=$info['meta_value'];
} elseif ($info['meta_key'] == 'ea_map_longitudine' &&
!isset($json_info[$info['ID']]['longitudine'])) {
$json_info[$info['ID']]['longitudine']=$info['meta_value'];
}
if (! isset($json_info[$info['ID']]['term_taxonomy_id']) ||
! in_array($info["term_taxonomy_id"],$json_info[$info['ID']]['term_taxonomy_id'])){
$json_info[$info['ID']]['term_taxonomy_id'][]=$info["term_taxonomy_id"];
}
}
echo json_encode($json_info);
die();
}