Accessing json does not work

后端 未结 3 1544
臣服心动
臣服心动 2021-01-28 22:32

I am using Ajax to receive a JSON update:

    $(document).ready(function(){
    $(\'form\').submit(function(event){
        event.preventDefault();
        var f         


        
3条回答
  •  自闭症患者
    2021-01-28 23:31

    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

提交回复
热议问题