Is 'length' null or not an object? IE 8

Hi guys, quick question: I get the following error: "length is zero or not an object" in IE 8, who has any ideas? Feedback is much appreciated ...

function refresh() { $.getJSON(files+"handler.php?action=view&load=update&time="+lastTimeInterval+"&username="+username+"&topic_id="+topic_id+"&t=" + (new Date()), function(json) { if(json.length) { for(i=0; i < json.length; i++) { $('#list').prepend(prepare(json[i])); $('#list-' + count).fadeIn(1500); } var j = i-1; lastTimeInterval = json[j].timestamp; } }); } 
+6
javascript jquery internet-explorer
source share
6 answers

Just check that the object is empty or empty:

 if (json && json.length) { // ... } 

C'mon gang was obvious :-)

+10
source share

JSON objects (returned by jQuery or otherwise) do not have a length property. You will need to iterate over the properties, most likely, or know the structure and just pull out what you want:

 $.getJSON( ..., ..., function( json ) { for( var prop in json ) { if( !json.hasOwnProperty( prop ) ) { return; } alert( prop + " : " + json[prop] ); } } ); 

Alternatively, take a library like json2 , and you can strengthen the object for output / debugging.

+1
source share

put the JSON in the range, then pin it and paste it here so we can see it:

 <span id="JSObject2">empty</span> 

with json2.js from here: (link for it at the bottom of the page) http://www.json.org/js.html

 myJSON = JSON.stringify(json); $('#JSObject2').text(myJSON); 

Using this, we can help you better, and you can see what you have!

+1
source share

JSON is an object, you seem to consider it as an array. Does it really have a length property? Show us JSON?

Instead, you may need for..in .

EDIT: Can you make JSON from the backend structure like this?

 ({ "foo": [] }) 
0
source share

What does your returned JSON look like? If you return an object, the length may not be explicitly defined, whereas if you return an array, it should be determined automatically.

0
source share

The first thing that comes to mind is that length is not a property of any json. What should be the json variable?

0
source share

All Articles