-1

This is my code:

<?php

$steamids = '76561197960435530';
$APIKEY = MYAPPKEY;

$steamAPI = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?steamids=$steamids&key=$APIKEY&format=json";
$json_object= file_get_contents($steamAPI);
header('Content-Type: application/json');
echo $json_object;

?>

I get everything what I need when I go to this info.php file. But how should I jQuery parse this JSON response ? I would like to do something like this:

function steamFunction(data){
    $("#userinfo").html('<img src="' +data.response.players.avatar +'">');
}

Is it possible somehow ?

This is my jQuery code:

$(document).ready(function(){

    $.getJSON("/info.php", function(json){
     $("#userinfo").html(json.response.players.avatar);
});
});

I'm not getting any errors but the image wont appearing. Also when I alert this I get undefined

feknuolis
  • 3
  • 3

2 Answers2

0

Try instead this code:-

    $.getJSON( "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?steamids=76561197960435530&key=52A66B13219F645834149F1A1180770A&format=json", function( json ) {
    console.log (json.response.players[0].avatar);


});

Cheers :)

Raef Kandil
  • 307
  • 1
  • 12
-1

Did you try $.parseJSON ?

$(document).ready(function(){
    $.getJSON("/info.php", function(json){
        json = $.parseJSON(json);
        $("#userinfo").html(json.response.players.avatar);
    });
});
  • I'm getting this: `Uncaught SyntaxError: Unexpected token o` – feknuolis Feb 24 '16 at 21:47
  • You should try to see what is the response you receive. You can use `console.log(json);` before and after the `json = $.parseJSON(json);` Maybe the input of the function is not what you expect ;) – Michaël HB Feb 25 '16 at 21:25