无法从由PHP脚本生成的JSON数据创建的JavaScript对象中获取属性
我有一个PHP脚本,可以查询DB并将结果作为JSON数据返回。该文件包含一些特定于Codeigniter的函数 此函数用于接收id并将表中的一些数据返回给JS代码无法从由PHP脚本生成的JSON数据创建的JavaScript对象中获取属性,php,jquery,ajax,json,codeigniter,Php,Jquery,Ajax,Json,Codeigniter,我有一个PHP脚本,可以查询DB并将结果作为JSON数据返回。该文件包含一些特定于Codeigniter的函数 此函数用于接收id并将表中的一些数据返回给JS代码 public function get_l($id){ //$id is not empty variable $this->db->where('id',$id); $q=$this->db->get('news'); $res = $q->result_array(); $this->out
public function get_l($id){
//$id is not empty variable
$this->db->where('id',$id);
$q=$this->db->get('news');
$res = $q->result_array();
$this->output->set_header("HTTP/1.0 200 OK");
$this->output->set_header("HTTP/1.1 200 OK");
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
$this->output->set_header("Content-Type: application/json; charset=utf-8");
$this->output->set_header("Pragma: no-cache");
$out = json_encode($res);
$this->output->set_output($out);
}
然后我需要用下一个JS代码处理JSON:
function getContent(id){
$.post('/admin_ajax/news/get',
{
id: id
},
function(result)
{
alert(result+"\n"+result.length);//this will output [object Object] 1
for (var key in result)
{
alert(key+':'+result[key]); //and this 0:[object Object]
}
alert(result.title); //here we are getting undefined
},
'json'
);
我在控制台中没有收到错误或警告。在firebug中,我看到了服务器返回的内容
HTTP标头:
Server nginx/1.1.19
Date Fri, 26 Oct 2012 11:59:12 GMT
Content-Type application/json; charset=utf-8
Content-Length 85
Connection keep-alive
X-Powered-By PHP/5.3.10-1ubuntu3.4
Cache-Control post-check=0, pre-check=0
Pragma no-cache
答复:
[{"id":"5","title":"test","alias":"test","content":"tes","date":"123","type":"test"}]
JSON:
我发现了一个类似的问题,但没有帮助。结果是一个包含一个元素的数组:
[
{
"id": "5",
"title": "test",
"alias": "test",
"content": "tes",
"date": "123",
"type": "test"
}
]
索引0处的元素包含所需的信息。您需要执行以下操作:
alert(result[0].id);
alert(result[0].title);
alert(result[0].alias);
结果是一个包含一个元素的数组:
[
{
"id": "5",
"title": "test",
"alias": "test",
"content": "tes",
"date": "123",
"type": "test"
}
]
索引0处的元素包含所需的信息。您需要执行以下操作:
alert(result[0].id);
alert(result[0].title);
alert(result[0].alias);
JSON对象位于数组[…]中,因此长度为1
function(result) {
var jsonObj = result[0];
JSON对象位于数组[…]中,因此长度为1
function(result) {
var jsonObj = result[0];
看起来您得到的响应实际上是一个包含一个元素json对象的数组 尝试使用结果[0],如下所示:
....
function(result)
{
alert(result[0]+"\n"+result[0].length);//this will output [object Object] 1
for (var key in result[0])
{
alert(key+':'+result[0][key]); //and this 0:[object Object]
}
alert(result[0].title); //here we are getting undefined
}
....
看起来您得到的响应实际上是一个包含一个元素json对象的数组 尝试使用结果[0],如下所示:
....
function(result)
{
alert(result[0]+"\n"+result[0].length);//this will output [object Object] 1
for (var key in result[0])
{
alert(key+':'+result[0][key]); //and this 0:[object Object]
}
alert(result[0].title); //here we are getting undefined
}
....
大量代码,但毫无疑问,您尝试过echo$out;而是$this->output->set_output$out;?如果您使用console.log而不使用alert,您会看到该对象吗?另一方面,它看起来像一个数组,我会尝试结果[0]。title?有很多代码,但毫无疑问,您是否尝试过使用echo$out;而是$this->output->set_output$out;?如果您使用console.log而不使用alert,您会看到该对象吗?另一方面,它看起来像一个数组,我会尝试结果[0]。标题?是的,这是一个数组。我可以使用数组的第一个元素访问属性。是的,这是一个数组。我可以使用数组的第一个元素访问属性。