views:

127

answers:

3

I'm requesting a json feed using the standard jquery getJson request. The json url is not hosted on my domain.

$.getJSON('myfeed.json?jsoncallback=?',

function(data){
 console.log(data);
})

However using Firebug, I'm not seeing any data logged in the console. But I can see the json data has been loaded as is available when I look under the Net panel in Firebug.

Anyone know why this is?

Sample JSON

[{
    "person": {
        "name": "Bob",
    }
},
{
    "person": {
        "name": "Dave",
    }
},
{
    "person": {
        "name": "Jim",
    }
}
}]
+1  A: 

Make sure that:

  1. The console is on at all. Type javascript:console.log('test') into the address bar to see if anything comes up - sometimes you need to refresh the page in order to get the console running properly.
  2. The feed on the other domain is, in fact, built for JSON-P, which is required when accessing feeds on other domains. What feed are you actually trying to access?
Matchu
How can I tell if the server can handle JSON-P?
Meander365
It will only work if the other feed is built specifically in JSON-P format. If it's traditional JSON, it will not. Are you working with a feed that is supposed to be public?
Matchu
I'm getting a 200 back from the server and when I inspect the requested url in firebug I can see a JSON tab with all the data objects there. So even though I can see this, I see can't access the ojbects in jQuery because its not, possibly, jsonp? That right?
Meander365
Could I see the feed URL? JSON-P is packaged like this: `callbackName([{'foo': 'bar'}, {'lol': 'wut'}])` - note the callback.
Matchu
Aahhh - no, it's not wrapped with a callback name. So you're right, it's not a json-p feed.
Meander365
If it's a major web API, it may offer a JSON-P feed in addition. Don't count it a loss before you check! :)
Matchu
A: 

I think the getJSON method doesn't parse the content unless you're content-type header is text/json. You can either change your server side header or perhaps do something like this

$.get("http://MYURL", function(data){
  var jsondata = eval("("+data+")")
}
leonm
A: 

Make sure that the json data is well formed and also that the server returns an appropriate content type. Place a breakpoint at the console.log. Most probably this isn't executed as the callback doesn't run for some reason.

kgiannakakis
It's well formed and the content type being returned is application/json; charset=utf-8
Meander365