Solved

reading a json response using jQuery $.ajax method

Posted on 2013-11-05
6
422 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

911 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

23 Experts available now in Live!

Get 1:1 Help Now