现在球员我知道这是一个简单的错误,但无论我尝试我无法访问json值,每当我使用警报后解析json它显示我undefined 为什么是由什么引起的? 这里是我的代码 脚本无法在codeigniter中访问我的ajax成功json值
function getfriend_requests()
{
var id=$('.id_data').attr('value');
jQuery.ajax({
type:'POST',
url:'<?php echo base_url("user/getallfriends"); ?>',
data:{id:id},
dataType:'json',
success:function(data)
{
var ParsedObject = JSON.stringify(data);
var json = $.parseJSON(ParsedObject);
alert(json);
$.each(json,function(key,data)
{
alert(data.object);
});
}
});
}
现在控制器
public function getallfriends()
{
$id=$this->input->post('id');
$this->load->model('Pmodel');
$data['senders']=$this->Pmodel->get_all_user_friends_sender($id);
$data['recievers']=$this->Pmodel->get_all_user_friends_reciever($id);
echo json_encode($data);
}
现在模型
public function get_all_user_friends_sender($id)
{
$this->db->select('*');
$this->db->from('user_friend');
$this->db->join('user_data', 'user_friend.senders_id = user_data.id');
$this->db->join('user', 'user_friend.senders_id = user.id');
$this->db->where('user_friend.senders_id',$id);
$query = $this->db->get();
$row = $query->result_array();
// print_r($row);
return($row);
}
public function get_all_user_friends_reciever($id)
{
$this->db->select('*');
$this->db->from('user_friend');
$this->db->join('user_data', 'user_friend.recievers_id = user_data.id');
$this->db->join('user', 'user_friend.recievers_id = user.id');
$this->db->where('user_friend.recievers_id',$id);
$query = $this->db->get();
$row = $query->result_array();
// print_r($row);
return($row);
}
现在,当我试图返回与result_array IY值显示我不确定值,但如果我使用$ row_array,它只会从每个模型中返回单个值。 你能告诉我我要去哪里吗?
为什么要将响应串联起来以便在下一行再次将它解析回json?你已经为你的ajax调用设置了'json'作为dataType,所以它应该已经是一个json对象。你还应该发布你从PHP返回的json响应。 –
在你的ajax调用的响应部分中做一个'console.log(data)'来查看你的浏览器必须说的内容 –
由于'$ query-> result_array()'你拥有'array的'json,所以在控制台上看到'console.log(data);'上的输出是什么。 –