If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Receiving AJAX /JSON Response & Parsing it (without JQuery)

Okay so the problem I seem to be having is that apparently my Javascript seems unable to receive my JSON responseText correctly. My console area in Google Chrome is not showing any errors for the syntax of the JSON that I have been sending back to the Javascript.

For error-checking purposes I created a small span at the bottom of my page called "notes_list" and had Javascript tell it to change the innerHTML of that area to whatever the length is, once the responseText was received and it said "undefined". Now I told it to "echo", for lack of better term, the value of JSON.parse(ajax.responseText) and it just says "[object Object]".

Why is Javascript having problems accessing the responseText array? When I manually set a variable to the responseText by literally copying and pasting it from FireBug (which shows the AJAX response) and started playing with it, it didn't seem to have any problems. So what gives? Here is the relevant code:

As you can see I am trying to loop through the array later on but I need to make sure Javascript is handling the response text and assigning to a variable correctly. Any help would be appreciated, thank you.

For error-checking purposes I created a small span at the bottom of my page called "notes_list" and had Javascript tell it to change the innerHTML of that area to whatever the length is, once the responseText was received and it said "undefined". Now I told it to "echo", for lack of better term, the value of JSON.parse(ajax.responseText) and it just says "[object Object]".

Hmmm then exactly what do I have to do in order to find the length (number of elements) in the first dimension of the responseText array? It gave me 'undefined' when I tried using responseText.length and I need that number for my loop (so I can loop through each first dimension of the array and access the second-dimension elements).

When in doubt, just plunk the JSON code into some JavaScript, directly, to find out what you have.

I just tested this out and it seems that it is working. I do not fully understand why Javascript is unable to loop through the JSON object as if it were an array, especially when the top-dimension variables / keys are numbers (ie easily sortable). Can anyone elaborate on this in layman's terms?

Thanks a ton, I will be able to use this syntax or something similar for future implementations of AJAX.

Edit: Now can someone tell me why the new images are not being added to the product boxes? The product boxes were all created but no <a> or <img> tags within them it doesn't seem. It *is* possible to add or change an image source with Javascript, right?

Also should I be giving each of the new product boxes a specific ID attribute? Or will that not be necessary?

Edit #2: I might have to try using another browser. When I try to use the console in Google Chrome it has no luck altering images at all (including setting src to "" or a different image). Can someone else try using the console in Chrome and try altering one of the product images?