I am using Ajax to receive a JSON update:
$(document).ready(function(){
$(\'form\').submit(function(event){
event.preventDefault();
var f
set the dataType
to json
so that the response is parsed
success: function(){
$.get('{{ path('PUSChatBundle_refresh') }}', function(data){
alert(data[0].text);
},'json'); //<-- specify the dataType
}
or manually parse the json
success: function(){
$.get('{{ path('PUSChatBundle_refresh') }}', function(data){
var json = $.parseJSON(data); //<- parse json
alert(json[0].text);
});
}
example:
var j='[{"messageId":43,"text":"ghstgh"}]';
var json = $.parseJSON(j);
console.log(json[0].text); // or alert(json[0].text);
DEMO
JavaScript arrays start at 0, not 1.
Your array only has one element, so you want to use 0
as your index:
alert(data[0].text);