2

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?

2
  • Very well-asked question. Specifics, code, some debugging you've already done, great stuff. One small thing for future questions: Instead of "I got some error" say what, exactly, the error is. Commented Jul 11, 2013 at 6:38
  • @T.J.Crowder okey thnx for your reply...:) Commented Jul 11, 2013 at 6:46

2 Answers 2

3

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;
     }
 });
Sign up to request clarification or add additional context in comments.

3 Comments

"Add dataType : 'json' to the ajax call" Or better yet, have the server return the correct Content-Type.
@MrCode when i alert json why i got only 0th element only?
By targeting abc[0].fname you are always reading the first. You can use a loop if you want to iterate over them all.
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;

1 Comment

This is just a duplicate of the answer that was here 10 minutes earlier. Also, please refer to the How to Format box next to the editing area when you were typing, and the preview area underneath where you could see what your answer would look like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.