0

I have this code:

function useHttpResponse() 
{

 if (xmlhttp.readyState==4 )
 {
 var response = eval('('+xmlhttp.responseText+')');
  alert(response);
 for(i=0;i<response.Users.length;i++)
        alert(response.Users[i].UserId);

 }
}

When i alert, the first alert is "[object Object]"

Why is that so? I need to remove that...how?

2 Answers 2

1

Decoding a JSON string converts it into a native JavaScript object. When you alert() it, the object's toString() method is called to cast the object back to a string. Any object cast to a string becomes [object Object]. Consider the following example:

var myObj = new Object();
alert (myObj);            // alerts [object Object]
alert (myObj.toString()); // alerts [object Object]
alert (({}).toString());  // alerts [object Object]

If you want to JSON encode the object again, you can use the JSON.stringify() method found in modern browsers and provided by json2.js.

var myObj = {"myProp":"Hello"};
alert (JSON.stringify(myObj));    // alerts {"myProp":"Hello"};
Sign up to request clarification or add additional context in comments.

Comments

0

Why is that so?

Because that is what you get when you convert a simple object to a string.

I need to remove that...how?

Delete alert(response); from your source

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.