Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

reading a json response using jQuery $.ajax method

Posted on 2013-11-05
6
437 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 33

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 33

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 33

Author Closing Comment

by:Big Monty
ID: 39625358
thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

807 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