• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 501
  • Last Modified:

reading a json response using jQuery $.ajax method

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
Big Monty
Asked:
Big Monty
  • 3
  • 2
1 Solution
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Author Commented:
i've also tried it without the if statement - still getting the same result
0
 
GaryCommented:
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
 
ManishLeadCommented:
I think data is array. Try alert(data[0]);
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
Big MontySenior Web Developer / CEO of ExchangeTree.org Author Commented:
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
 
GaryCommented:
The square brackets declare it as an element of an array
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Author Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now