help - how to get data from json like this:
Closed this issue · 2 comments
marti1125 commented
var search = $("#username").val();
$.ajax({
url: 'https://coderwall.com/' + search + '.json?callback=?',
dataType: "json",
timeout: 5000,
success: function (data) {
$.each(data, function (key, val) {
var username = val.username;
var name = val.name;
var location = val.location;
var github = val.accounts.github;
$('#result').append('<h1>' + name + '</h1>');
$('#result').append('<h2>' + location + '</h2>');
$('#result').append('<p>Github account: <a href=https://github.com/' + github + ' target=_blank>' + github + '</a></p>');
$.each(val.badges, function (key2, val2) {
var badge_name = val2.name;
var badge_desp = val2.description;
var badge_img = val2.badge;
$('#bimg').append('<li class="bassdge"><figure><img src=' + badge_img + '/><figcaption><p id=despt >' + badge_desp + '</p><b class=name>' + badge_name + '</b></figcaption></figure></li>');
});
});
},
error: function (data) {
alert('Username does not found');
}
});
Deleted user commented
You might want to try this snippet:
jQuery.ajax({
url: 'https://coderwall.com/' + jQuery('#username').val() + '.json?callback=?',
dataType: 'JSONP',
success: function(response) {
// you might want to dump the response to firebug or the chrome console
// console.debug(response)
jQuery('#result').append('<h1>' + response.data.name + '</h1>');
// add rest of your code here
},
error: function(response) {
alert('Username not found');
}
});
By the way, try to avoid multiple var's in the same scope..
var username = val.username,
name = val.name,
location = val.location,
github = val.accounts.github;
Or even better, as you already have the response object: Don't shift around values if you don't have to. You can access all the values by:
alert(response.data.name);
alert(response.data.location);
// and so on..