Sai Krishna
Sai Krishna

Reputation: 557

Javascript accessing a nested object from ajax response

enter image description here

response._bodyText works, but I get undefined when I use response._bodyText.result

Upvotes: 0

Views: 164

Answers (4)

Rupal
Rupal

Reputation: 1109

First you have to parse that because its in json format. Try

data = JSON.parse(response._bodyText)
data.result

For Reference you can study about JSON

Upvotes: 1

user9246109
user9246109

Reputation:

response._bodyText.result is undefined because _bodyText is a string.

You must first turn _bodyText into an object:

var body = JSON.parse(response._bodyText);

After that body.result should work.

Upvotes: 0

nitte93
nitte93

Reputation: 1840

What you have inside response._bodyText is a stringified JSON. To access the keys inside of it. you need to first parse it using JSON.parse

Example: JSON.parse('{ "hello":"world" }')

This will give you an object {hello: 'world'}

In your case you do JSON.parse upon response._bodyText, then you can access it just like a normal object.

JSON.parse(response._bodyText)

Upvotes: 0

Aswin Ramesh
Aswin Ramesh

Reputation: 1674

you need to parse it

try

JSON.parse(response._bodyText).result

Upvotes: 0

Related Questions