tags:

views:

223

answers:

1

I have a javascript array object like

{arr = {"0":{"name":"henry","role":"user"},"1":{"name":"mark","role":"admin"}}

I have a html which requests this array from my server( by including a script tag with url to my server function that serves this array}

The problem is that When I run my HTML file in firefox, it easily detects this array and does the further processing with it but On IE , when I try to access the elements of the array, it throws an exception (object is null or undefined).

Any idea why element detection could be failing in IE.

How do we find out the properties of an array object in IE?

+1  A: 

That is not really an array, is simply an object literal with numeric properties, you should iterate it by using the for...in statement:

var arr = {"0":{"name":"henry","role":"user"},
           "1":{"name":"mark","role":"admin"}};

for (var key in arr) {
  if (arr.hasOwnProperty(key)) {
    // value = arr[key];
  }
}

But I think you should simply return a real Array:

var arr = [{"name":"henry","role":"user"},
           {"name":"mark","role":"admin"}];
CMS
The issue is that when i try to serve the array form my PHP code to the javascript file, IE treats it as a string and not an array. thats why it's failing. I wrote in my post "Yet if I try to check the array length (in IE) , it shows the correct length." This is not correct..I checked back this code and it shows string length and NOT the array length. So basically IE is not able to detect it as an array.
Annibigi