?
Solved

reading a json response using jQuery $.ajax method

Posted on 2013-11-05
6
Medium Priority
?
490 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 34

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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 34

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 34

Author Closing Comment

by:Big Monty
ID: 39625358
thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
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)
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

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