简体   繁体   中英

Use JQuery to get data from JSON file

Overflow!

I'm currently working on a little application I have to finish for school monday. I didn't have a lot of time to make something big. So I decided, why not retrieve information of Steam's Web Api and get the stats of players.

The url to the steam api: http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=DA697BB2D106697D5F8AC7E7A2BFAC52&steamid=76561198263871727

The last parameter &steamid= represents the id of the player. Now I have found out how to get the steamid into a variable, but when trying to add the id to the rest of the url ( http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=DA697BB2D106697D5F8AC7E7A2BFAC52&steamid= id should be here and fetching the data with the Ajax.getJson method.. It just doesn't work.. I'm for very experienced with JSON btw.. Can someone help me out with this?

My Web Page:

<!DOCTYPE html>
<html lang="en">
    <head>
        <!--Meta Information-->
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <!--JQuery Mobile-->
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
        <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

        <!--FontAwesome-->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">

        <!--Custom Styles-->
        <link href="css/style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div data-role="page" id="index">
            <div data-role="header">
                <h1>CS:GO Stats</h1>    
            </div>
            <div data-role="main" class="ui-content">
                <div class="search">
                    <label for="search">Enter SteamID:</label>
                    <input type="search" name="search" id="inputSearch" />
                    <input type="submit" id="butSearch" data-inline="true" value="Search SteamID">
                </div>
                <div id="result">

                </div>
            </div>    
        </div>
        <!--getSteamUserId function-->
        <script src="js/getSteamUserId.js"></script>
    </body>
</html>

My Javascript Code:

$(document).ready(function() {

    $('#butSearch').click(function(event) {
        var input = $('#inputSearch').val();

        $.getJSON( "http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=DA697BB2D106697D5F8AC7E7A2BFAC52&steamid=" + input, function( data ) {
            var items = [];
            $.each( data, function( key, val ) {
                items.push( "<li id='" + key + "'>" + val + "</li>" );
            });

            $( "<ul/>", {
                "class": "my-new-list",
                html: items.join( "" )
            }).appendTo( "#result" );
        });
    })

})

Now what I want is to get the stats data from the JSON file into my web page. Would anyone know how to do this? I can also see the JSON is not only a flat JSON file.. Like it has different layers (if that's a good explenation)..

Thanks in advance!

Work with jsonP like here:

$.ajax({
  url: url,
  dataType: 'JSONP',
  type: 'GET',
  jsonp: 'jsonp',
  success: handler

});

Working example here

I'm not entirely sure about the first part. It gives me an error which after googling led me to "No 'Access-Control-Allow-Origin' header is present on the requested resource" which advises using CORS . Error:

XMLHttpRequest cannot load http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=DA697BB2D106697D5F8AC7E7A2BFAC52&steamid=&76561198263871727 . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 400.

Once you have the JSON it's easier. If it's stringified you can turn it into a javascript object with

steamObject = JSON.parse(steamJsonData);

and then navigate through it like a normal javascript object. Cycle through playerstats.stats[i] with a for loop and you can add the data to your page using normal DOM manipulation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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