使用jquery和ajax解析xml url

时间:2021-09-10 00:25:35

I am trying to do an web application displaying sports data. From some websites I was able to archive the data and the format is in XML.

我正在尝试显示体育数据的Web应用程序。从一些网站我能够存档数据,格式是XML格式。

I tried to research and figure out how to parse the data using jquery ajax but failed. Below is I was trying to parse the xml data using the cross domain ajax plugin (http://bit.ly/Jbi9iX).

我试图研究并弄清楚如何使用jquery ajax解析数据但失败了。下面是我试图使用跨域ajax插件(http://bit.ly/Jbi9iX)解析xml数据。

$(document).ready(function(){
    $.ajax({
    type: 'GET',
    url: 'http://api.sportradar.us/nba-t3/games/6b163da6-80c2-4a5a-85bd-27a67fbd49f6/boxscore.xml?api_key=x5ht86hhx763tnt9ugbdgjsf',
    crossDomain: true,
    dataType: 'jsonp',
    success: parseXml(data),
    error: function(){alert("Error: Something went wrong");}
    });
});

function parseXml(xml){
    $(xml).find('item').each(function(){
        $("#output").append($(this).attr('name').text());
    });
}

The html is as follows:

html如下:

<!DOCTYPE html>
<head>
<title>Untitled Document</title>
<script src = "jquery-1.11.1.js"></script>
<script src = "jquery.xdomainajax.js"></script>
<script src = "XMLReader.js"></script>
</head>

<body>
<p id ="output">as</p>
</body>
</html>

1 个解决方案

#1


0  

you have to get this via php try this way...

你必须通过PHP尝试这样尝试...

source.php

<?
    header('Content-type: application/json');
    $json=file_get_contents("http://api.sportradar.us/nba-t3/games/6b163da6-80c2-4a5a-85bd-27a67fbd49f6/boxscore.xml?api_key=x5ht86hhx763tnt9ugbdgjsf");
    echo $json;

    ?>

Js file

$(document).ready(function(){
    $.ajax({
    type: 'GET',
    url: 'source.php',
    dataType: 'json',
    success: parseXml(data),
    error: function(){alert("Error: Something went wrong");}
    });
});

#1


0  

you have to get this via php try this way...

你必须通过PHP尝试这样尝试...

source.php

<?
    header('Content-type: application/json');
    $json=file_get_contents("http://api.sportradar.us/nba-t3/games/6b163da6-80c2-4a5a-85bd-27a67fbd49f6/boxscore.xml?api_key=x5ht86hhx763tnt9ugbdgjsf");
    echo $json;

    ?>

Js file

$(document).ready(function(){
    $.ajax({
    type: 'GET',
    url: 'source.php',
    dataType: 'json',
    success: parseXml(data),
    error: function(){alert("Error: Something went wrong");}
    });
});