0

I have a problem with javascript, im calling an ajax method that returns this string:

{
 "ObjectResponse": {
 "Operation": "OK",
 "Response": "SUCCESS",
 "Message": "List of AAA Found",
 "List": [
 {
 "keySource": "gat\/images\/images_set\/apple.jpg",
 "idSiteKey": "1",
 "text": "Apple"
 },
 {
 "keySource": "gat\/images\/images_set\/cat.jpg",
 "idSiteKey": "2",
 "text": "Cat"
 },
 {
 "keySource": "gat\/images\/images_set\/coffee.jpg",
 "idSiteKey": "3",
 "text": "Coffee"
 },
 {
 "keySource": "gat\/images\/images_set\/dog.jpg",
 "idSiteKey": "4",
 "text": "Dog"
 },
 {
 "keySource": "gat\/images\/images_set\/horse.jpg",
 "idSiteKey": "5",
 "text": "Horse"
 },
 {
 "keySource": "gat\/images\/images_set\/police.jpg",
 "idSiteKey": "6",
 "text": "Police"
 },
 {
 "keySource": "gat\/images\/images_set\/tree.jpg",
 "idSiteKey": "7",
 "text": "Tree"
 }
 ]
 }
}

I assing the content in this way:

xhr.onreadystatechange = ensureReadiness; 
....
responseText = xhr.responseText;

If i try to parse it on javascript with:

response = JSON.parse(responseText);

if I acces a property such response.ObjectResponse.Operation I do get the right content.. but when I try to access the List it allways brakes

and if I try the same String but instead of calling the service I assign the content to a var it works I do can access the List

var myTemporalString ='{"ObjectResponse":{"Operation":"OK","Response":"SUCCESS","Message":"List of Keys Found","List":...';
response.JSON.parse(myTemporalString);

Any suggestion why this could be happening?

I Hate Lazy
49k13 gold badges89 silver badges79 bronze badges
asked Oct 26, 2012 at 20:24
4
  • This responseText = xhr.responseText; shoud be done inside ensureReadiness. You can only use the response from there. Commented Oct 26, 2012 at 20:27
  • 1
    It's definitely not related to the actual XHR response. What happens when you try to access the .List? What do you mean by "it breaks"? Commented Oct 26, 2012 at 20:27
  • You should probably post more code. Commented Oct 26, 2012 at 20:28
  • List is an array of object so you should use List[0]['keySource'] to get the first object and List[1]['keySource'] for the second and so on. Commented Oct 26, 2012 at 20:31

2 Answers 2

1

You can try this way,

 xhr.onreadystatechange = function() {
 if (xhr.readyState == 4) {
 if (xhr.status == 200) {
 try{
 var mJsonData = JSON.parse(xhr.responseText);
 }catch(err){
 console.log(err);
 alert(err);
 return;
 }
 for(i=0;i<jsondata.ObjectResponse.List.length;i++){
 console.log(jsondata.ObjectResponse.List[i].text);
 console.log(jsondata.ObjectResponse.List[i].keySource);
 console.log(jsondata.ObjectResponse.List[i]. idSiteKey);
 }
 }
 }
 }
answered Oct 26, 2012 at 20:41
Sign up to request clarification or add additional context in comments.

Comments

0

Use a loop!

var array = response.ObjectResponse.List;
var len = array.length;
for(i=0; i<len; i++) {
 //Use array[i] to access the list
 array[i].keySource
 array[i].idSiteKey
 array[i].text
}
answered Oct 26, 2012 at 20:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.