我是codeigniter框架和json的新手。我想在用户点击网址时从数据库中获取数据,但是我收到了错误。
我的剧本:
function abc(i)
{
if(i == 1)
{
$.ajax({
type: "post",
url: "<?php echo base_url('welcome/liststd');?>",
data: 'postData='+ i ,
success: function(json)
{
var abc = json;
document.getElementById("fname").innerHTML=abc[0].fname;
}
});
}
}
Firebug回复:
[
{
"id":"31",
"fname":"Darshan",
"mname":"D",
"lname":"Dave",
"std":"1",
"marks":"12000",
"image":"image31.jpg",
"id1":"1",
"in_date":"0000-00-00",
"upd_date":"2013-07-10"
},
{
"id":"34",
"fname":"Darshan",
"mname":"D",
"lname":"Dave",
"std":"1",
"marks":"12000",
"image":"image34.jpg",
"id1":"1",
"in_date":"2013-07-06",
"upd_date":"2013-07-09"
}
]
当我在json
函数中提醒success
时,它会向我显示:
[
{
"id":"31",
"fname":"Darshan",
"mname":"D",
"lname":"Dave",
"std":"1",
"marks":"12000",
"image":"image31.jpg",
"id1":"1",
"in_date":"0000-00-00",
"upd_date":"2013-07-10"
},
我的错误是什么?
答案 0 :(得分:3)
您没有解析JSON。将dataType : 'json'
添加到ajax调用中。这将告诉jQuery自动为您解析JSON。
$.ajax({
type: "post",
url: "<?php echo base_url('welcome/liststd');?>",
data: 'postData=' + i,
dataType: 'json', // <----- tell jQuery to parse the JSON
success: function (json) {
var abc = json;
document.getElementById("fname").innerHTML = abc[0].fname;
}
});
答案 1 :(得分:0)
try this in your response...
also in your $.ajax tell jquery to parse json
dataType: 'json'
in your response..
var response = JSON.parse(json);
response.fname;