0
I need to get the element authorDisplayName
of that array json
. He’s the return of a requisition ajax
. How to take only the element with jquery
or javascript
?
"kind": "youtube#commentThread",
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/chdu9X44b3BNrN9QUEyKNGH_eiA\"",
"id": "z12ts1iicov0ybbex22syzij4tuwyrxyk04",
"snippet": {
"channelId": "id",
"videoId": "id",
"topLevelComment": {
"kind": "youtube#comment",
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/gKHFh_4gWxRa4aGZKqb5E1DJnww\"",
"id": "z12ts1iicov0ybbex22syzij4tuwyrxyk04",
"snippet": {
"authorDisplayName": "Nome do autor",
"authorProfileImageUrl": "imagem",
"authorChannelUrl": "LINK",
"authorChannelId": {
"value": "UCLw8RgF4mQXkA_-ZCXAAyIQ"
},
"channelId": "UCIwspRtKNszHhIhl36gREjQ",
"videoId": "VYw3eYIOJ08",
"textDisplay": "",
"textOriginal": "r",
"canRate": true,
"viewerRating": "none",
"likeCount": 0,
"moderationStatus": "likelySpam",
"publishedAt": "2016-10-05T19:23:16.000Z",
"updatedAt": "2016-10-05T19:23:16.000Z"
}
},
"canReply": true,
"totalReplyCount": 0,
"isPublic": true
}
}
My code is like this
$(document).on('click','.enviarComentario', function(){
var comentario = $('.comentbox').val();
var id = $(this).attr('videoId');
$.ajax({
type:'post',
url: 'enviarComentario.php',
data : { "comentario" : comentario, "idVideo" : id},
success: function (e) {
alert(e);
var res = e;
alert(res);
var authorDisplayName = res.snippet.channelId;
alert(authorDisplayName);
var comentarios = $('.comentariosLista').html();
$('.comentariosLista').html("");
$('.comentariosLista').html('<div class="media">'+
'<div class="media-left">'+
'<a href="#">'+
'<img class="media-object" src="'+res.snippet.topLevelComment.snippet.authorProfileImageUrl+'" alt="...">'+
'</a>'+
'</div>'+
'<div class="media-body">'+
'<h4 class="media-heading">'+
'<strong>'+e.snippet.topLevelComment.snippet.authorDisplayName+'</strong>'+
'</h4>'+comentario+'</div></div>');
},
error: function(){
alert('n deu comentario');
}
});
});
Maybe your $.ajax request doesn’t understand that your result is a JSON. You can force this by using
'dataType': 'json'
just below thetype: post
.– Lucas Ferreira
and don’t forget to use in Google Chrome or Firefox, the tab
Network
Developer Tools so you can "see" how the request is coming from your server, so there is no PHP error there returning a poorly formed JSON.– Lucas Ferreira
Apparently your answer is being treated as String. Try to give a var json = JSON.parse(res) var authorDisplayName = json.snippet.channelId;
– Lucas Brogni