Solved

reading a json response using jQuery $.ajax method

Posted on 2013-11-05
6
417 Views
Last Modified: 2013-11-05
I'm having some trouble reading my json response, not sure where i'm going wrong.

$.ajax({
                type: "POST",
                url: url,
                dataType: "json",
                data: dataString, 
                success: function (data) {
                    if (data != 0)              // will return 0 if no records are found
                        alert(data.title);
                    else
                        alert('Error Message for no records goes here');
                }
            });

Open in new window


i keep getting an undefined value when i run this. When looking into the console, i see the following JSON response, which I validated on http://jsonformatter.curiousconcept.com/ :

[
   {
      "recordID":101,
      "userID":100,
      "title":"couch",
      "description":"a very groovy looking couch!",
      "available":false,
      "createDate":"11/1/2013",
      "fileName":null,
      "fileDescription":null
   }
]

Open in new window


i also tried using $.parseJSON on the success call, but that generated other error messages, which I think is due to the fact the the data returned is already a json object.

Any help is greatly appreciated
0
Comment
Question by:Big Monty
  • 3
  • 2
6 Comments
 
LVL 32

Author Comment

by:Big Monty
ID: 39625282
i've also tried it without the if statement - still getting the same result
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39625304
Because you are returning an array of the values, so just point to the first array index (if this is how the actual results look)

data[0].title
0
 
LVL 11

Expert Comment

by:Manish
ID: 39625324
I think data is array. Try alert(data[0]);
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 32

Author Comment

by:Big Monty
ID: 39625338
that seems to work, thanks.

I don't understand why it's working that way though, all of the examples I've seen shows it as a straight up object
0
 
LVL 58

Expert Comment

by:Gary
ID: 39625346
The square brackets declare it as an element of an array
0
 
LVL 32

Author Closing Comment

by:Big Monty
ID: 39625358
thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now