简体   繁体   English

通过跨站点AJAX解释JSONP

[英]Interpreting JSONP via cross-site AJAX

I'm trying to extract some data from the Premier League Fantasy Football site and falling short on what feels like a catch 22. 我正在尝试从英超联赛梦幻足球网站上提取一些数据,但未能达到22的目标。

My AJAX JSONP script looks like the following: 我的AJAX JSONP脚本如下所示:

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 这将生成错误SyntaxError: missing ; before statement SyntaxError: missing ; before statement

I believe because of the accepted answer on this page: AJAX call and clean JSON but Syntax Error: missing ; 我相信由于此页面上的可接受答案: AJAX调用和干净的JSON但语法错误:missing; before statement 声明前

Changing the dataType to json means I fall foul of the same origin policy as described there. 将dataType更改为json意味着我违反了此处所述的相同原始策略。

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. 让我感到讨厌的是,当我使用JSONP版本时,状态为200,并且可以在Firefox调试器中看到完整的“对象”结构。

So what is it that Firefox is doing to get at the data that I'm not? 那么,Firefox正在做什么以获取我不是的数据?

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. 显然,Firefox不是使用JS来获取数据,所以最终我也没有。

PHP cURL did the trick: PHP cURL达到了目的:

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM