json数据的JavaScript问题

时间:2014-05-02 04:37:22

标签: javascript ajax json

我正在尝试获取和解析json数据,因此我可以将其输出到空白HTML文件。我不断遇到的问题是,如果我获取数据并解析它,我会收到Uncaught TypeError: Cannot read property 'SOCENGPRE' of undefined错误。如果我以dataType: "text"格式获取json数据,则会出现XMLHttpRequest cannot load <api url> No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.错误。该网站位于本地计算机上,由于个人原因,我无法编辑服务器进行跨平台处理。代码如下所示:

var myDataVar;
function main() {
    $.ajax({
        url: "http://api&leagues=SOCENGPRE&format=jsonp&callback=?",
        dataType: "json",
        success: function (data) {
            myDataVar = $.parseJSON(data);
            getUpcomingFixtures();
        }
    });
}
function getUpcomingFixtures() {
    for (var i = 0; i <= myDataVar.SOCENGPRE.fixtures.length - 1; i++) {
        console.log(myDataVar.SOCENGPRE.fixtures[i].id);
        console.log(myDataVar.SOCENGPRE.fixtures[i].home_team + " vs " + myDataVar.SOCENGPRE.fixtures[i].away_team);
    }
}

获取数据的示例如下所示:

{
    "SOCENGPRE": {
        "league_name": "Barclays Premier League",
        "league_phid": null,
        "league_type": null,
        "fixtures": [{
            "id": "64714",
            "code": "SOCENGPRE",
            "event_slug": "west_ham-tottenham-1401290",
            "start": "1399117500",
            "home_team": "West Ham",
            "home_team_phid": null,
            "home_team_logo": "\/\/dxnxhx88pdxyv.cloudfront.net\/logo\/32\/t523.png",
            "home_team_short": "",
            "away_team": "Tottenham",
            "away_team_phid": null,
            "away_team_logo": "\/\/dxnxhx88pdxyv.cloudfront.net\/logo\/32\/t498.png",
            "away_team_short": "",
            "phid": null
        }, {
            "id": "64711",
            "code": "SOCENGPRE",
            "event_slug": "manchester_u-sunderland-1401286",
            "start": "1399125600",
            "home_team": "Manchester U",
            "home_team_phid": null,
            "home_team_logo": "\/\/dxnxhx88pdxyv.cloudfront.net\/logo\/32\/t20790.png",
            "home_team_short": "Man U",
            "away_team": "Sunderland",
            "away_team_phid": null,
            "away_team_logo": "\/\/dxnxhx88pdxyv.cloudfront.net\/logo\/32\/t382.png",
            "away_team_short": "",
            "phid": null
        }, {
            "id": "64712",
            "code": "SOCENGPRE",
            "event_slug": "stoke-fulham-1401288",
            "start": "1399125600",
            "home_team": "Stoke",
            "home_team_phid": null,
            "home_team_logo": "\/\/dxnxhx88pdxyv.cloudfront.net\/logo\/32\/t389.png",
            "home_team_short": "",
            "away_team": "Fulham",
            "away_team_phid": null,
            "away_team_logo": "\/\/dxnxhx88pdxyv.cloudfront.net\/logo\/32\/t379.png",
            "away_team_short": "",
            "phid": null
        }]
    }
}

我的目标是获取夹具ID并且两个团队竞争。我有什么问题吗?

1 个答案:

答案 0 :(得分:0)

尝试使用下面的代码,不要再处理JSON,因为它已经处理好了,假设string是你的JSON对象。

在脚本开头将此变量结果声明为数组;

var result = [];


function getUpcomingFixtures() {
  $.each( string , function ( i , val) {

     $.each( val['fixtures'] , function ( k , fixturesData) {

      result.push( { id: fixturesData.id , 
                     hometeam : fixturesData.home_team , 
                     awayteam : fixturesData.away_team
                    } 
                  );
      });

  });
}