1

when I did

loadPeople(){

    this.myService.load().then(data => {
            this.people = data;
            alert(this.people);
        });
    }

it alerts json as :

 {
"status": "true",
"statusCode": 200,
"response": [{
    "user_id": "92",
    "firstname": "joy",
    "lastname": "Panchal",
    "email": "[email protected]",
    "password": "7Y7+K0vZIVWPDUQH++Iu+/+tMZ",
    "user_type_id": "1"
}, {
    "user_id": "89",
    "firstname": "mark",
    "lastname": "haris",
    "email": "[email protected]",
    "password": "4JICqnTkR8ysTI+nQQ+rpfAf7e",
    "user_type_id": "1"
}]

}

now i am trying to access "response" by

loadPeople(){

this.myService.load().then(data => {
        this.people = data.response;
        alert(this.people);
    });
}

but it alerts as "undefined" .

can anyone tell where i am missing ??

4
  • Should work perfectly fine. jsfiddle.net/ADukg/10046 Commented Feb 13, 2017 at 10:03
  • yes it should , but dont know what went wrong . alert still saying "undefined" Commented Feb 13, 2017 at 10:07
  • probably this.people is not yet filled. Commented Feb 13, 2017 at 10:08
  • thanks matheno , i will prefer your suggestion Commented Feb 13, 2017 at 10:16

2 Answers 2

1

you can access like this. please try.

`loadPeople(){
  this.myService.load().then(data => {
    this.people = JSON.parse(data.response);
    alert(this.people);
});

}`

Sign up to request clarification or add additional context in comments.

Comments

0

you need parse JSON first:

this.myService.load().then(data => {
    let res = JSON.parse(data);
    this.people = res.response;
    alert(this.people);
  });
}

6 Comments

Thanks for your response, it says " Property 'response' does not exist on type 'string'. " as it is JSON element.
could you console.log your data variable
yes, console.log it shawing when i am trying with " this.people = data " , not happening the same for " this.people = data.response "
your data is object or json string
its JSON stirng
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.