3

I want to get the JSON data from the URL below and use it in my webpage in any way possible, can it be done?

http://fantasy.premierleague.com/web/api/elements/100/

(Note I do not own this site, I'm just using the data for a simple free app.)

If I simply enter the URL into the browser it works but that's no good as I want my page to use the data, so an AJAX request would be ideal but I cannot get it to work. I've tried using a jQuery AJAX call but cannot get it to work.

I'm staring to think it's not possible from within the browser and I would need to use Mechanize or something like that, which is not really an option.

Thanks.

user1947215
  • 33
  • 1
  • 1
  • 3
  • That data is not JSON, but JSONP. – Šime Vidas Jan 04 '13 at 01:13
  • This post might help you: http://stackoverflow.com/q/3595515/221781 – izilotti Jan 04 '13 at 01:13
  • There's something very odd going on with their JSONP. If I add a callback to it, it seems to cache it server-side, even when sending a cache killer. You're going to have endless problems unless you ask the creators of the API what's going on. – jeremyharris Jan 04 '13 at 01:36

1 Answers1

8

I had this working, and now the error function is getting executed with a 200 status code. Is it possible they limit the number of queries per x seconds because it is working intermittently.

Edit: Something is going on with their caching. Stuff is jacked.

$.ajax({
    type:"GET", 
    url: "http://fantasy.premierleague.com/web/api/elements/100/", 
    success: function(data) {
            $("body").append(JSON.stringify(data));
        }, 
    error: function(jqXHR, textStatus, errorThrown) {
            alert(jqXHR.status);
        },
   dataType: "jsonp"
});​​​​​​​​​​​​​​​
James Kleeh
  • 12,094
  • 5
  • 34
  • 61
  • Here is an example I found of using the API by using Firebug. It seems that it puts the current time in milliseconds after the URL. Here's an example: http://fantasy.premierleague.com/web/api/elements/415/?_=1357337284504 – user1947215 Jan 04 '13 at 22:06
  • I used the following code to create the timestamp but still having problems: `var d = new Date();var epocTime = ( d.getTime() - d.getMilliseconds() );` and then this `url: 'http://fantasy.premierleague.com/web/api/elements/100/?_=' + epocTime` – user1947215 Jan 04 '13 at 22:14
  • 1
    I know it's been many years since this answer but can you remove the invisible characters at the end of the code snippet? Causes an illegal character error in the browser. Should be just after `});`​​​​​​​​​​​​​​​ and hit backspace a few times. – KevinLamb Jul 12 '19 at 18:14