-1

Is there something wrong with my JSON?

http://codepen.io/anon/pen/Ieyvl

var url = "http://x.com/json.js";

$.getJSON(url, function(response){
    alert("worked!");
    alert(response);
});

If its a "cross-domain" issue, then why does this work?

http://codepen.io/anon/pen/BHshC

var url = "http://gdata.youtube.com/feeds/api/playlists/PL3E245DF445E37F50?v=2&alt=jsonc";

$.getJSON(url, function(response){
    alert("worked!");
    alert(response);
});
5
  • use console.log(response); Commented Jan 23, 2013 at 15:48
  • 1
    What's the domain of the page executing that code? You may be running up against the same origin policy. Commented Jan 23, 2013 at 15:49
  • No.. just checked it at jsonlint.com and it passed.. so it should be fine.. It's probably a cross domain issue Commented Jan 23, 2013 at 15:49
  • 1
    json.js - probably not the greatest name for json data Commented Jan 23, 2013 at 15:50
  • in second example: response headers: Access-Control-Allow-Origin:* Commented Jan 23, 2013 at 15:54

4 Answers 4

3

Look at the console:

XMLHttpRequest cannot load http://xml.hosting.subsplash.com/5KQ4CM/json.js. Origin http://secure.codepen.io is not allowed by Access-Control-Allow-Origin.

Read about the same origin policy.

You need to use JSONP if they support it. And if the server supports it look into CORS


[EDIT]

I did not check, but if the Google example works, it is probably because Google Enabled CORS for that resource. That mean browsers that support it can request the resource with an Ajax call. Not all browsers support that type of handshake request. [looking at you older IEs]

Sign up to request clarification or add additional context in comments.

Comments

0

It's valid JSON, maybe jquery has problems because it returns mimetype application/javascript instead of application/json

1 Comment

+1 THIS is true too.. It's trying to load it as application/javascript
0

If you read jQuery.getJSON() document at http://api.jquery.com/jQuery.getJSON/,
it says following: Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol. Also to remember: as of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently.

Comments

0

The hosting server isn't handling the response type correctly, in addition you need to do this as JSONP or if using getJSON add ?callback=? to your url at the end.

var url = "http://xml.hosting.subsplash.com/5KQ4CM/json.js?callback=?";

$.getJSON(url, function(response){
    alert("worked!");
    alert(response);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.