I have this code:
$.getJSON('http://www.reapp.se/cmesapp/cmes.json?callback=?', function(data){
alert("disco");
}).error(function() {alert("fail!";});
Which is sending out an alert with “fail” instead of “disco” and the problem is the URL, why, I have no idea!
This is very wierd (btw its my domain so is the error there i can fix it) because I can access other external domains with this code… I can add that the JSON is validated (check for yourself) and that I’ve been searching… It’s not working with this approach:
jquery getJSON not working on url
Thanks in advance!
Ok, after extensive searching I’ve finally came up with a answer!
I should, before I continue, use this code for an iphone-app so I can’t guarantee that the following is cross-browser safe.
First I take my .json document hosted on my server which is external from the server this code is operating (in my case a phone) and changes it to .js (I don’t know if this matters). Secondly I put the whole JSON – object in paranthesis and then I assign it to a variable I create, like this:
Now the variable v is a valid javascript variable. After that you just import the script to your DOM dynamically through (let’s say the url is “http://www.disco.com/chabs.js”:
First I grab the object from the DOM then I create a new script-element. Then I make the script-element a javascript, set the source to the url and the onload is called when the load is finished, this is necessary or the document won’t be loaded when you call “v”, finally I add the jsonScript to .
After searching alot of different pages and jquery – documentation this was the simplest and probably best solution.