views:

287

answers:

6

I'm attempting to parse JSON using jQuery and I'm running into issues. Using the code below, the data keeps coming back null:

<!DOCTYPE html>
<html>
  <head>
    <title>JSON Test</title>
  </head>
  <body>
    <div id="msg"></div>
    <script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
    <script>
        $.ajax({
          url: 'http://datawarehouse.hrsa.gov/ReleaseTest/HGDWDataWebService/HGDWDataService.aspx?service=HC&zip=20002&radius=10&filter=8357&format=JSON',
          type: 'GET',
          dataType: 'json',
          success: function(data) {
            $('#msg').html(data[0].title); // Always null in Firefox/Chrome. Works in IE8.
          },
          error: function(data) {
            alert(data);
          }
        });
    </script>
  </body>
</html>

The JSON results look like the following:

{"title":"HEALTHPOINT TYEE CAMPUS","link":"http://www.healthpointchc.org","id":"tag:datawarehouse.hrsa.gov,2010-04-29:/8357","org":"HEALTHPOINT TYEE CAMPUS","address":{"street-address":"4424 S. 188TH St.","locality":"Seatac","region":"Washington","postal-code":"98188-5028"},"tel":"206-444-7746","category":"Service Delivery Site","location":"47.4344818181818 -122.277672727273","update":"2010-04-28T00:00:00-05:00"}

If I replace my URL with the Flickr API URL (http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&amp;tagmode=any&amp;format=json&amp;jsoncallback=?), I get back a valid JSON result that I am able to make use of.

I have successfully validated my JSON at JSONLint, so I've run out of ideas as to what I might be doing wrong.

Any thoughts?

Update: I had the client switch the content type to application/json. Unfortunately, I'm still experiencing the exact same problem. I also updated my HTML and included the live URL I've been working with.

Update 2: I just gave this a try in IE8 and it works fine. For some reason, it doesn't work in either Firefox 3.6.3 or Chrome 4.1.249.1064 (45376). I did notice a mistake with the data being returned (the developer is returning a collection of data, even for queries that will always return a single record), but it still baffles me why it doesn't work in other browsers.

It might be important to note that I am working from an HTML file on my local file system. I thought it might be a XSS issue, but that doesn't explain why Flickr works.

+3  A: 

Have you considered using the more flexible $.ajax? I would break it down there and see if the default $.getJSON is not providing you with exactly what you need.

$.ajax({
   url: 'results.json',
   type: 'GET',
   dataType: 'json',
   success: function(data, status)
   {
       alert(data);
   }
});

Would be the equivalent ajax 'parent' implementation. Try mucking around and see if there is a specific property you need to set.

Tejs
I took this suggestion and updated the code above.
senfo
What about adding a handler for an error case also to see if that's what's happening?
justkt
That's actually something I hadn't heard of, before. I implemented it this way: http://api.jquery.com/ajaxError/Is that what you had in mind? I updated my HTML (above) to show my implementation.
senfo
You would add `error: function(data: status){ alert(data);}`
Tejs
I gave this a try. Unfortunately, there doesn't seem to be any errors, so nothing runs.
senfo
+1  A: 

if one url (the flickr url) works, while another doesnt (your own), i would say the problem is with your url.

It looks like you are using a relative path, is that your intention? have you used firebug's net panel to see what the request looks like?

mkoryak
If I view the Response tab off the Net panel in Firebug, I see JSON-formatted data only when I use the Flickr URL. Otherwise, the response stream seems to be empty. I **do** see the expected results if I connect to port 80 via telnet and manually perform a GET.
senfo
+3  A: 

Is the content served as "application/json"?

ZippyV
Very good question. My guess is no (a client of mine wrote the JSON service). I'll have to look into that.
senfo
I'm almost certain this is the answer. Just checked and it's Content-Type: application/atom+xml; charset=utf-8.
senfo
+5  A: 

Flickr's API supports JSONP, whereas the one you're connecting to does not.

jQuery sees that =? and understands there's a request for a JSONP callback and creates one. You can see it on line 5003 of the jQuery library your sample page uses.

So, you need to change two things

  1. Add a callback parameter to your request. Whatever you (or your developer) wants to call it. Let's say you choose cb - so add this to the AJAX request: &cb=? (this way we let jQuery handle the creation of a callback for us)
  2. Have your developer that made the service accept this new parameter, and "pad" the result with it before returning it.
Peter Bailey
Thank you very much for the reply. I would have never thought of that (I've actually never heard of JSONP prior to this). I'm waiting on another developer to make the changes to the service. After I have verified it works, I'll accept this as the correct answer.
senfo
A: 

Have you tried!?

DEMO: http://jsbin.com/uboso3/edit

$(function() {
  var data = {
"title": "HEALTHPOINT TYEE CAMPUS",
"link": "http://www.healthpointchc.org",
"id": "tag:datawarehouse.hrsa.gov,2010-04-29:/8357",
"org": "HEALTHPOINT TYEE CAMPUS",
"address": {
    "street": "4424 S. 188TH St.",
    "locality": "Seatac",
    "region": "Washington",
    "postal": "98188-5028"
},
"tel": "206-444-7746",
"category": "Service Delivery Site",
"location": "47.4344818181818 -122.277672727273",
"update": "2010-04-28T00:00:00-05:00"};

  //var data = $.parseJSON( data ); //if you put the data between double quote!

 $('#msg').append('<ul>' +
                  '<li>' + data.title + '</li>' +
                  '<li>' + data.link + '</li>' +
                  '<li>' + data.id + '</li>' +
                  '<li>' + data.org + '</li>' +
                  '<li>' + 'Address:' +
                  '<ul>' + 
                  '<li>' + data.address.street + '</li>' +
                  '<li>' + data.address.locality + '</li>' +
                  '<li>' + data.address.region + '</li>' +
                  '<li>' + data.address.postal + '</li>' +
                  '</ul>' +
                  '</li>' + 
                  '<li>' + data.tel + '</li>' +
                  '<li>' + data.category + '</li>' +
                  '<li>' + data.location + '</li>' +
                  '<li>' + data.update + '</li>' +
                  '</ul>'
                 );

});

NOTE: you need to rename street-address to street and postal-code to postal

aSeptik
A: 

This is a case of Crossdomain request error. Flickr Works , because it is JSONP , while the other url does not work because it is not JSONP enabled. 1. Check if the url supports JsonP 2. if not, create a proxy web service in your local domain (the same domain as the web page is) 3. Call that proxy webservice which in turn calls the external url from the server side.

Sundararajan S