【问题标题】:extract external site's json data提取外部站点的json数据
【发布时间】:2015-01-21 16:08:22
【问题描述】:

我想在我的网站上显示有多少玩家在其他扑克网站上玩。 This link 以 JSON (tournaments.summary.players) 格式提供我需要的数据。我使用 .getJSON 请求,但它似乎没有检索数据。这是我使用的脚本:

// url used to get the JSON data
var pkrStats = "http://www.psimg.com/datafeed/dyn_banners/summary.json.js";

$.getJSON(pkrStats, function (json) {

    // Set the variables from the results
    var playersN = json.tournaments.summary.players;
    console.log('Total players : ', playersN);

    // Set the table td text
    $('#players').text(playersN);

});

// Caching the link jquery object
var $myLink = $('a.myLink');

// Set the links properties
$myLink.prop({
    href: pkrStats,
    title: 'Click on this link to open in a new window.'
}).click(function (e) {
    e.preventDefault();
    window.open(this.href, '_blank');
});
body {
    font-size: 75%;
    font-family:"Segoe UI", Verdana, Helvetica, Sans-Serif;
}
#body {
    clear: both;
    margin: 0 auto;
    max-width: 534px;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
    margin-top: 0.75em;
    border: 0 none;
    margin-top:35px;
}
#body td {
    padding:15px 30px 15px 10px;
}
#body tr td:nth-child(1) {
    font-weight:bold;
}
#address {
    width:400px;
}
<div id="body">

    <hr/>
    <table border="1">
        <tr>
            <td>Url:</td>
            <td><a class="myLink">json.js</a> 
            </td>
        </tr>
        <tr>
            <td>Users playing:</td>
            <td id="players"></td>
        </tr>
    </table>
</div>

【问题讨论】:

  • 我不确定,但它不应该是启用 JSONP/CORS 的请求吗?

标签: javascript jquery html json


【解决方案1】:

如果您查看控制台,您可能会看到类似这样的内容

“XMLHttpRequest 无法加载 http://www.psimg.com/datafeed/dyn_banners/summary.json.js。请求的资源上不存在 'Access-Control-Allow-Origin' 标头。因此不允许访问 Origin 'null'。”

这意味着您尝试下载的链接未启用跨源资源共享。

更多Get a JSON file from URL and display查看此答案

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 2012-07-02
    • 2015-08-27
    相关资源
    最近更新 更多