1

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!

Community
  • 1
  • 1
Johan S
  • 3,531
  • 6
  • 35
  • 63

2 Answers2

1

I think that the error is that the URL is not returning JSONP only JSON. So jQuery will have hard to link the entity with your request.

Execute the code you wrote in Chrome and go to Network tab and see the response. Do the same for http://api.jquery.com/jQuery.getJSON/ and you will se that the later will have wrapped the answer into something like this: jQuery171043198914267122746_1327059063134({... code })

Simon Edström
  • 6,461
  • 7
  • 32
  • 52
  • I don't have Chrome installed on my ubuntu, have you gotten it to work? Yeah I've read about JSONP too but how do I change the content of my URL to return JSONP instead of JSON? – Johan S Jan 20 '12 at 11:55
  • I dont know which language you using on serverside. But just take the ?callback parameter from the querystring and wrapp the whole answer. Mark this questtion as answered and create a new question about how to get your JSON answer support JSONP. Somebody maybe good give you an example if you point out which language you are using on the serverside. – Simon Edström Jan 20 '12 at 12:01
  • I'm not using any serverside language other than a FTP where i dragged in the cmes.json file... I'm lost here! The question isnt answered yet because I'm wondering what differs cmes.json from the other files that its working with. – Johan S Jan 20 '12 at 12:38
  • Well, lets do like this. Just put this in the beginning of your file: jQuery( and add an extra ) at the end. After that you need to get jQuery to handle a static callback, have a look at http://stackoverflow.com/questions/1608302/can-i-use-a-static-i-e-predetermined-callback-function-name-when-requesting (Your callback should be jQuery) – Simon Edström Jan 20 '12 at 12:49
  • @Simon: That would override the global `jQuery` function and is probably not a good idea. Use any other function name instead. @Reapp: Just use a fixed callback name and set up your Ajax call to use that name. There is a lot of information in the jQuery documentation. – Felix Kling Jan 20 '12 at 13:00
0

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:

var v = (...JSON UNMODIFIED...);

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":

var head = document.getElementsByTagName('head')[0];
var jsonScript = document.createElement('script');
jsonScript.type = 'text/javascript';
jsonScript.src = 'http://www.disco.chabs.js';
jsonScript.onload = callback;
head.appendChild(jsonScript);

function callback(){
    //use v here as a js/json - object
}

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.

Johan S
  • 3,531
  • 6
  • 35
  • 63