S
S
Sergei2014-11-08 09:54:11
JavaScript
Sergei, 2014-11-08 09:54:11

Why can't I access the property of an object in the response from the server?

Good afternoon. I'm trying to figure out ajax. I can not access the properties of the response object from the server. When outputting to the console, the object itself is displayed, when trying to get its property - undefined. How so? ... Here is a piece of the function where I am trying to apply:

$.ajax({
          type: 'POST',
          url: '/add.php',
          data: msg,
          success: function(data) {   
          console.log("Ответ json:",data);  
          console.log(data.w_client);     
          },
          error:  function(xhr, str){
                alert('Возникла ошибка: ' + xhr.responseCode);
            }
        });


Console screenshot:
987bc532eed242faa6a38ebb3fc66dd0.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
wkololo_4ever, 2014-11-08
@Neversmille

Use JSON.parse(data);
learn.javascript.ru/json

S
Sergey, 2016-12-20
@Neversmille

Or just specify the dataType in the ajax request parameters. Then JSON.parse won't need to be written

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question