0

enter image description here

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

3
  • What do you mean response._bodyText works? Can you do console.log(response._bodyText)? What do you see? (You might have to JSON.stringify it.) Commented Jan 21, 2018 at 16:11
  • Are you using a library for this? XMLHttpRequest itself does not have a _bodyText property. If you ARE using a library then you MUST realize that there is an informal coding convention in js that variables that begin with underscore is to be considered private so users MUST NOT USE _bodyText. Look at the object's prototype in __proto. There must be a method to return the response body. Do not use _bodyText Commented Jan 21, 2018 at 16:21
  • Better yet, read the library's documentation Commented Jan 21, 2018 at 16:22

4 Answers 4

1

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

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

2 Comments

Thank you.This works. Why doesn't JSON.parse(response) work?
Because response is just a object. So you don't need to parse it. You can simply access it by key like response._bodyText.
0

you need to parse it

try

JSON.parse(response._bodyText).result

Comments

0

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)

Comments

0

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.

Comments

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.