1

I'm trying to extract some data from the Premier League Fantasy Football site and falling short on what feels like a catch 22.

My AJAX JSONP script looks like the following:

function getPlayer(playerNumber) {
$.ajax({
    url: 'http://fantasy.premierleague.com/web/api/elements/' + playerNumber + '/',
    dataType: 'jsonp',
    success : function(responseText) {
        alert(responseText);
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
        if (XMLHttpRequest.status != 200)
            alert('getPlayer failed!');
    },
    complete : function(jqXHR) {
        alert('complete');
    }
});

}

This generates the error SyntaxError: missing ; before statement

I believe because of the accepted answer on this page: AJAX call and clean JSON but Syntax Error: missing ; before statement

Changing the dataType to json means I fall foul of the same origin policy as described there.

The thing that irks me is that when I use the JSONP version, I get status 200 and I can see the full 'object' structure in my Firefox debugger.

So what is it that Firefox is doing to get at the data that I'm not?

Community
  • 1
  • 1
Leon Clements
  • 95
  • 1
  • 7
  • You can not make a site support jsonp, they NEED to support it on their end. – epascarello Jan 31 '16 at 14:35
  • 1
    How do you explain how Firefox shows him the data then? – mariocatch Jan 31 '16 at 14:43
  • Understood epascarello, thanks. I'm just curious as to what's going on in FF. – Leon Clements Jan 31 '16 at 14:53
  • @mariocatch You can not make a server return JSONP by slapping random JSONP callback on it. I can stick any JavaScrip file on the page which is what a JSONP call does. What the browser does is stick `` on the page. So of course the code will be stuck on, but you are not getting the functionName() wrapped around it like JSONP expects. – epascarello Jan 31 '16 at 14:53
  • I understand :) I'm asking how his FF is returning the response he's after. – mariocatch Jan 31 '16 at 14:54
  • Seeing it in the debugger, does not mean Firefox is returning it. The site does not support CORS so OP is out of luck with a JavaScript frontend solution. A proxy would be needed. – epascarello Jan 31 '16 at 14:56

1 Answers1

0

Okay seems I was coming at this issue from the wrong angle. Obviously Firefox aren't using JS to obtain the data so eventually neither did I.

PHP cURL did the trick:

$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, 'http://fantasy.premierleague.com/web/api/elements/' . $playerId);
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curlSession);
$player = json_decode($result, true);
curl_close($curlSession);
Leon Clements
  • 95
  • 1
  • 7