{'id_user':'1', 'time':'123456340', 'message':'Da, privet'} {'id_user':'145', 'time':'123456123', 'message':'Privet'} 

these lines come back from

 each(result, value){ } 

inside the loop, I want to get the value.time value - but writes underfined

  • 2
    Full feature is possible? And see what console.log() says - lampa
  • console.log (value) gives the result: {"id_user": "145", "time": "123456123", "message": "Privet"} but console.log (value.time) gives underfined - IOleg
  • one
    And typeof value? - ReinRaus
  • one
    $ .ajax ({type: 'POST', url: '/ chat / GetDialog', data: {idUser: idUser}, success: function (data) {$ .each (JSON.parse (data), function (index, value) {console.log (value);});}}); - IOleg
  • Answer: 0: "{'id_user': '1', 'time': '123456340', 'message': 'Da, privet'}" 1: "{'id_user': '145', 'time': ' 123456123 ',' message ':' Privet '} "2:" {"id_user": "145", "time": "123456123", "message": "Privet"} " - IOleg

1 answer 1

.

 JSON.parse( value ).time 
  • php side: echo json_encode ($ messages); die (); in $ messages I have a line in json format - IOleg
  • @Ioleg you understand why? I think it is important to clarify why the data still needed to be driven through the parse () function. - lampa
  • explain - it does not hurt. As understood, to parse a string that is a json record and to get access to the json element - IOleg
  • @Ioleg is true! But in the $.ajax() method there is a hack! You need to add the property: dataType: 'json' and the data in the function will come as a json object. - lampa