0

I am trying to write a HTML5 mobile application and use jQuery to get a json from the url http://cin.ufpe.br/~rvcam/favours.json I tried using

var url='http://cin.ufpe.br/~rvcam/favours.json';
$.getJSON(url, function(data, status)
    {
        console.log(data);
        console.log(status);
    });

but nothing shows up on the console. I don't see what I am doing wrong.

[EDIT] I learned from another post that I can't normally retrieve information from another server. But this server in particular (cin.ufpe.br/~rvcam) is mine. Can I use PHP or some other method to allow my application to retrieve the data?

13
  • possible duplicate of $.getJSON syntax issue Commented Mar 23, 2014 at 0:36
  • 1
    Try adding a .fail() callback and see if it's raising an error. The arguments will match $.ajax()'s error option -- (xhr, status, error). Commented Mar 23, 2014 at 0:38
  • 1
    @user2918054 It is possible, depending on the clients you need to support. But will require 2 edits: The "padding" of foo() will need to be removed from the file so that it's just JSON and the server will need to respond with CORS headers that allow the request. Commented Mar 23, 2014 at 1:05
  • 2
    $.ajax(url, {dataType:"jsonp",jsonpCallback:"foo"}).done(function(data) {...}); will tell jQuery to expect jsonp with the callback name foo. Commented Mar 23, 2014 at 1:19
  • 1
    @rvcam: Then you have to configure your server so that it can read the callback name from the URL and create the response dynamically. That won't work though if you are using a static file for that data. Commented Mar 23, 2014 at 1:44

2 Answers 2

5

The URL doesn't return valid json. It returns some JavaScript that attempts to execute a function called "foo" and passes the object as an argument. This is commonly called "jsonp". It is a method of achieving cross domain ajax calls

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

2 Comments

And how would I use $.getJSON to make these cross domain ajax calls?
api.jquery.com/jQuery.ajax if you take a look at the section here for "jsonp" and "jsonpCallback" you should be able to figure out how to define foo and get the data
2

Your http://cin.ufpe.br/~rvcam/favours.json file isn't valid json. The valid json is wrapped in foo(). Remove the foo() from that file and it will work.

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.