Reputation: 247
I am new in codeigniter framework and json. I want to fetch data from the database when the user clicks on a url, but I'm getting an error.
My script:
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 Response:
[
{
"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"
}
]
When I alert json
in the success
function it shows me this:
[
{
"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"
},
What is my mistake?
Upvotes: 2
Views: 115
Reputation: 544
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;
Upvotes: 0
Reputation: 64526
You aren't parsing the JSON. Add dataType : 'json'
to the ajax call. This will tell jQuery to automatically parse the JSON for you.
$.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;
}
});
Upvotes: 3