By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,106 Members | 2,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,106 IT Pros & Developers. It's quick & easy.

javascript not identifying JSON Response as JSON

100+
P: 101
//Outputs:
Hi ,

I am making a application in which I have to send a request using Ajax to ther server and the server return response in JSON .

I just wanna know i am getting the following response from the server:

Expand|Select|Wrap|Line Numbers
  1. {"length":50,
  2. "accounting":[
  3. {"firstName":"John","lastName":"Doe","age":23},
  4. {"firstName":"Mary","lastName":"Smith","age":32}],
  5. "sales":[
  6. {"firstName":"Sally","lastName":"Green","age":27},
  7. {"firstName":"Jim","lastName":"Galley","age":41}
  8. ]}
In JavaScript how i accesss it. Means when I am tring to access it it is saying undefined

Expand|Select|Wrap|Line Numbers
  1. if (xmlHttp.readyState==4)
  2. {
  3. var JSONResponse = xmlHttp.responseText;
  4. alert (JSONResponse)
  5. document.getElementById("txtHint").innerHTML=JSONResponse.length;
  6. }
  7.  
alert is printing
Expand|Select|Wrap|Line Numbers
  1. {"length":50,
  2. "accounting":[
  3. {"firstName":"John","lastName":"Doe","age":23},
  4. {"firstName":"Mary","lastName":"Smith","age":32}],
  5. "sales":[
  6. {"firstName":"Sally","lastName":"Green","age":27},
  7. {"firstName":"Jim","lastName":"Galley","age":41}
  8. ]}
but document.getElementById("txtHint").innerHTML=JSONR esponse.length; is printing undefined

Please suggest what I am doing wrong in this.


Regards,
Nov 5 '07 #1
Share this Question
Share on Google+
5 Replies


gits
Expert Mod 5K+
P: 5,333
hi ...

change the line:

Expand|Select|Wrap|Line Numbers
  1. var JSONResponse = xmlHttp.responseText;
  2.  
to:

Expand|Select|Wrap|Line Numbers
  1. var JSONResponse = eval(xmlHttp.responseText);
kind regards
Nov 5 '07 #2

100+
P: 101
hi ...

change the line:

Expand|Select|Wrap|Line Numbers
  1. var JSONResponse = xmlHttp.responseText;
  2.  
to:

Expand|Select|Wrap|Line Numbers
  1. var JSONResponse = eval(xmlHttp.responseText);
kind regards
Now it is showing error expected ';' at
Expand|Select|Wrap|Line Numbers
  1.  var JSONResponse = eval(xmlHttp.responseText);
Nov 6 '07 #3

gits
Expert Mod 5K+
P: 5,333
so you have to show what you have done besides that, because when you test the following:

Expand|Select|Wrap|Line Numbers
  1. var test = eval({"length":50,
  2. "accounting":[
  3. {"firstName":"John","lastName":"Doe","age":23},
  4. {"firstName":"Mary","lastName":"Smith","age":32}],
  5. "sales":[
  6. {"firstName":"Sally","lastName":"Green","age":27},
  7. {"firstName":"Jim","lastName":"Galley","age":41}
  8. ]});
  9.  
  10. alert(test.length);
it alerts 50 as expected.

kind regards
Nov 6 '07 #4

100+
P: 101
it works Thanks ! :)
Nov 6 '07 #5

gits
Expert Mod 5K+
P: 5,333
glad to hear that :) ... post back to the forum anytime you have more questions ...

kind regards
Nov 6 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.