tags:

views:

63

answers:

4

Hi,

I'm trying to use jQuery.getJSON to get data from a domain I don't own myself. I've made it work in the past, but this time it keeps failing. I get the response into the browser, but then Chrome says SyntaxError: Unexpected token : and Firefox says invalid label.

There are a few peculiarities with this. Say the code is MY_JSON_RESPONSE (not a variable, just an "alias"). These both return the expected objects:

a = MY_JSON_RESPONSE;
eval(MY_JSON_RESPONSE);

This is the request I make:

$.getJSON("http://xxx.yyy.zzz/abc?callback=?",
   function(data) {
     console.log(data);
   });

EDIT: Below is the anonymised response data:

{"aaaaaaa":"aaaaaaaa aaaaa aaa aaaaaaaaaa","aaaaa":"aaaaaaaa aaaaa aaa","aa":"0000000","aaaa_aaaaaaa":"aaa aaaaa","aaaaaaa_aaaaaaa":"aa aaa aaaaaaaa aa aaaaaaaa aaa aaaaaaa aaaaaa aaa aaa aaaaaaaa aa aaaaaaaa aa aaaaaa aaaaa aaa aaaaa. aaa aaaaaaaa aaaaa aaa aaaaaaaaaa aaaaaaaa aaaaa aaaaa aaaaaaaa aaaaaaaaaaa aaaaaaaaaa aa aaaa 00 aaaaaaa aaaaaaaaa aa aaa aaaaaaaa aaaaa aaa, aa aaaaaaaaaaa aaaaaaaaaaaaa aaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaa, aaaaaaaaa aaa aaaaaaa aaaaaaaa. 00% aa aaaaaaa aaaaaa aaaaa aaaaa aaaaaaaaaaaaa aaaaa aaaaaaaaaaaaa.","aaaaaaaaa":[{"aaaaaa":"aaaaa aaa aaaaaa  aaaaaaaa","aaaaaaa":"aaaaaaaa aa aaaaaa aaa aaaa","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":"\u00a00.00"},{"aaaaaa":"aaa","aaaaaaa":"aaaaaaaa aaaa aa &aaaa;aaaaa aaa aaaaaaaaaa aaaaaaaaa&aaaa; - aaa aaa aa, aaaaa aaa a aaaa aaaaa!","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":"\u00a000.00"},{"aaaaaa":"aaaaa","aaaaaaa":"aaaa aaaa aaaa!!!!","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":"\u00a00.00"},{"aaaaaa":"aaa\/aaa","aaaaaaa":"aaaaaaa aaaaa aaaa aaa aaaa aaaaa aaa aaaaa aaa aaaaaaa aaaa!","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaaaaa","aaaaaaa":"","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aa (aaaaaaa &aaa; aaaaaaaaa)","aaaaaaa":"","aaaa":"00\/00\/00","aaaaaa":"\u00a0000.00","aaaa_aaa":""},{"aaaaaa":"aaaaa  aaaaaaaa","aaaaaaa":"","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":"\u00a00.00"},{"aaaaaa":"aaaaaa aaaaaa","aaaaaaa":"","aaaa":"00\/00\/00","aaaaaa":"","aaaa_aaa":""},{"aaaaaa":"aaaaaa aaaaaaaa","aaaaaaa":"aaaa aaaaaaa aaaaaaaaa","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaa aaaaaaaaaaa","aaaaaaa":"aaaaa aaaaaa�a aaa, aaa! aaaa aaaaaaaa aa aaa aaa! :-) --aaa","aaaa":"00\/00\/00","aaaaaa":"","aaaa_aaa":""},{"aaaaaa":"aaaaaa aaaa - aaaaaaaa aaaaaa aaa","aaaaaaa":"aaaaaa aa aaa aaa aaaaaaaa aaaaa aaaaaa aa aaaa aa aaa aaaaa aaa aaaaa. aaaa aaaa aaaaaa aaaaaaaaa !!","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaa aaaaaaaaa","aaaaaaa":"aa aaaaaa aaaa aaaaaaaa aa aaaa aa aaaa aa a aaaa aaaaa aaa aaaaaaaaaa. aaaa aa aa aaaaaa aa a aaaa aaaaa.","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaaaaa aaa aaaaaaaaaaa aaaaaaa aaaaaa","aaaaaaa":"aaaaa aaaaa. aaaa aa aaa aaaaaa aaaaaaaaa!!","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaa  aaaaa","aaaaaaa":"","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":"\u00a00.00"},{"aaaaaa":"aaaaaaaaa aaaaaa","aaaaaaa":"aaaaaaa aaa aaaa aaaaaa�a aaaaaaaaa aaaa","aaaa":"00\/00\/00","aaaaaa":"\u00a0000.00","aaaa_aaa":"\u00a000.00"},{"aaaaaa":"aaaaaaa aaaaaa","aaaaaaa":"aaaaaaaaa aaaaa, aaa! aaaaaaa &aaa; aaaaa","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":"\u00a00.00"},{"aaaaaa":"aaaaa aaaaaaaa","aaaaaaa":"aaaaaaaa aaaaaaa - aaaaaaa aaaaaaaaa a aaa aaa aaaaaa!","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaa aaaaaa","aaaaaaa":"aaaaaaaaaaaa - aaa!","aaaa":"00\/00\/00","aaaaaa":"\u00a0000.00","aaaa_aaa":""},{"aaaaaa":"aaaaa aaaaaaaaaaa","aaaaaaa":"aaaa aaaa!!!","aaaa":"00\/00\/00","aaaaaa":"\u00a00.00","aaaa_aaa":""},{"aaaaaa":"aaaaa aaaaaaa","aaaaaaa":"aaaa aaaa aaaaa, aaaaaaa, aaaaaaa, aaaaa, aaa, aaaa, aaa, aaaaaaaa, aaaa, aaa, aaaaa, aaa, aaa, aaaaa, aaa, aaaa, aaaa, aaaa, aaaaa, aaa, aaaaaaaa","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaaaa aaa","aaaaaaa":"","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaa aaaaaa","aaaaaaa":"aaaa aaa aa aa aaaa aa a aaaaaaa aa aaaaa! aaa aaa aaaaa!","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaaa aaaaaaa","aaaaaaa":"","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaa aaaaaaaa","aaaaaaa":"aa aa aa! :)","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaa aaaaaaaaa","aaaaaaa":"aaaa a aaaaaa aaaaaaaaa aaaa aa aa aaa :)","aaaa":"00\/00\/00","aaaaaa":"","aaaa_aaa":""},{"aaaaaa":"aaa-aaaaaaa aaaaaaaa","aaaaaaa":"aaaa aaaaaaaa aa aa aaa aaaa aa aa aaaaaaaaaaa, aaaa a. aaaaaaaa","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaaa aaaa","aaaaaaa":"","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaaaaaa aaaa","aaaaaaa":"a aaaaaaaa aa aaa aaaaaaaa aaaaa aaa aa aaaaa aa aaaaa aaa aaaaa aaaaa  aa aaa aaaaaa aa aaaaaaaaa.","aaaa":"00\/00\/00","aaaaaa":"","aaaa_aaa":""},{"aaaaaa":"aaaaaaa aaaaaaa","aaaaaaa":"aaaaa aaaaaaaa aaa aaaa aaaaaaa aaaa aaaaaa aaa!","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":""},{"aaaaaa":"aaa aaaa","aaaaaaa":"aaaa aaaa aaaa aaaa aaaaaaaaaaa aaaa aaa aaa aaaaaaaaa aaaa aaa aa","aaaa":"00\/00\/00","aaaaaa":"\u00a000.00","aaaa_aaa":"\u00a00.00"}],"aaaa_aaaaaaa":"","aaaa_aaa":"\u00a0000.00","aaaaaaaaa_aaaaaa":"\u00a00,000.00","aaaaaaaaa_aaaaa":"\u00a00,000.00","aaaaaaa_aaaa":"aaaa:\/\/aaa.aaaaaaaaaa.aaa\/\/aaaaa\/aaaaaaa.aaaa?aaaa=aaaaaaa&aaaaaaaaa=aaaaaaaaaaa&aaaaa=000&aaaaaa=000&aaa=aaaa_aaaa0.aaa","aaaaaaaaa_aaaaaa":"\u00a00,000.00","aaaaaaaaa_aaaaaaa":"\u00a00.00","aaa_aaaaaa":true,"aaaaaaaaaa_aaaaaaaa":00}

I'd like to avoid having to do it on the server on my own domain, to minimise server load.

A: 

It is just not well formed JSON. Either that JSON string misses to quote all keys or it is an error with ((un)escaped) quotings in general.

jAndy
+1  A: 

The JSON parsing in jQuery 1.4 is more strict than past versions. Depending on your browser, it may use native JSON parsing, which is different than eval. One big difference is that now, all keys MUST be enclosed in double quotes, not single quotes. eval would do it with no error as you said, since JS isn't strict about quoting property names at all, but it will fail JSON parsing.

I had an issue with this myself when converting a project to jq 1.4, as I had some older code that was building json strings manually. For instance

return "{'a':12}";  // fails parsing due to single quotes

It is much better to use the built-in json libraries:

return '{"a":12}'; // works
return json_encode(array('a'=>12)); // preferred!
return json.dumps({'a':12}) # etc.

as they always produce valid json.

Alex JL
I think you're right, there are a few unescaped values in the response data. The problem is that I don't own the domain I'm requesting the data from, so is there a way to intercept jQuery's JSONP processing before it calls it's built-in eval function?
Andreas Jansson
You could get the data as a string, then call eval or json.parse on it.
Alex JL
Thanks, the problem seems to be in the way jQuery handles JSONP. I don't know the internals, but it appears that it fetches it like a script, and then the browser manages to find some syntax issue and throws the error, before handing control over to jQuery. Neither beforeSend() or dataFilter() are triggered. I guess I'll just have to do the processing on my own server.
Andreas Jansson
I've had issues with jSONP and jQuery that weren't apparent at first, too, like how you can't make JSONP calls synchronous. They're definitely handled differently.
Alex JL
A: 

The JSON sample is invalid. It include this substring:

}, {

That is, it ends one object and starts another. So it appears to be an array of objects except without enclosing square brackets.

Edit: my mistake! It does in fact have a nested array in it. Maybe you should "pretty print" it when you generate it, so that it is easier to see where the problem lies?

Daniel Earwicker
A: 

Interestingly, according to http://jsonformatter.curiousconcept.com/ that JSON is valid.

As others have noted ... jQuery 1.4 will not accept malformed JSON, earlier versions will however (if you want to go direction for some crazy reason.)

How are you calling the actual JSON data elements in code? I've run into problems a couple of times ... perfectly valid JSON, but isn't not easily called in jQuery.

An example: the tumblr (blog engine) API ... for some reason they used ... "-" ... so for the "body" of the blog post they have "regular-body" ... which jQuery aparently hates with a passion.

You'd think you could do ...

json.posts[2].regular-body

But that will error, so you need [] ...

json.posts[2]['regular-body']

So ...

If you can give in more detail about how you are calling the JSON data elements it might be helpful?

Justin Jenkins
I'm not able to query individual elements in the JSON data as the script dies on error within the getJSON function.
Andreas Jansson