Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过跨站点AJAX解释JSONP_Javascript_Jquery_Ajax_Jsonp - Fatal编程技术网

Javascript 通过跨站点AJAX解释JSONP

Javascript 通过跨站点AJAX解释JSONP,javascript,jquery,ajax,jsonp,Javascript,Jquery,Ajax,Jsonp,我正试图从英超梦幻足球网站上提取一些数据,但没有达到第22条军规的要求 我的AJAX JSONP脚本如下所示: function getPlayer(playerNumber) { $.ajax({ url: 'http://fantasy.premierleague.com/web/api/elements/' + playerNumber + '/', dataType: 'jsonp', success : function(responseText) {

我正试图从英超梦幻足球网站上提取一些数据,但没有达到第22条军规的要求

我的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');
    }
});
}

这将生成错误
SyntaxError:missing;在语句之前

我相信,由于这一页上的公认答案:

将数据类型更改为json意味着我违反了此处所述的同源策略

让我恼火的是,当我使用JSONP版本时,我的状态是200,我可以在我的Firefox调试器中看到完整的“对象”结构


那么Firefox在做什么来获取我没有的数据呢?

好吧,我似乎从错误的角度来看待这个问题。显然Firefox没有使用JS获取数据,所以最终我也没有

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

好吧,看来我从错误的角度来看这个问题。显然Firefox没有使用JS获取数据,所以最终我也没有

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

你不能让一个站点支持jsonp,他们需要在他们的一端支持它。你如何解释Firefox是如何向他显示数据的?理解了,谢谢。我只是好奇FF中发生了什么。@mariocch您不能通过对服务器执行随机JSONP回调来让它返回JSONP。我可以在页面上粘贴任何JavaScrip文件,这就是JSONP调用所做的。浏览器所做的是在页面上粘贴
。因此,代码当然会被卡住,但你并没有像JSONP所期望的那样将functionName()包装起来。我理解:)我是问他的FF如何返回他想要的响应。你不能让一个站点支持JSONP,他们需要支持它。你如何解释Firefox是如何向他显示数据的?理解Epasarello,谢谢我只是好奇FF中发生了什么。@mariocch您不能通过对服务器执行随机JSONP回调来让它返回JSONP。我可以在页面上粘贴任何JavaScrip文件,这就是JSONP调用所做的。浏览器所做的是在页面上粘贴
。因此,代码当然会被卡住,但您并没有像JSONP所期望的那样将functionName()包装起来。我理解:)我是在问他的FF如何返回他想要的响应。