views:

209

answers:

6

Hey guys quick question I get the following error 'length is null or not an object' in IE 8, anyone have any ideas? Feedback greatly 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;
    }
  });

}
A: 

A JSON is an object, you seem to be treating it like an array. Does it really have a length property? Show us the JSON?

You might need to use a for..in instead.

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

({
    "foo": [] 
})
meder
how can I show the raw json in this function?
Scarface
it just says [object Object],[object Object],[object Object],
Scarface
when I use alert(json); after if(json.length) {
Scarface
Please show us your RAW JSON.
meder
I am not exactly sure what you mean from making the json from the backend structure. I am gathering the information from a php document which gets the information from the database and puts it into a json array like so $data[] = $row; $out = json_encode($data); print $out;
Scarface
Why can't you just show us that real JSON? Or mimic it exactly.
meder
I did lol see the comment I made to Mark Schultheiss, after he showed me how to actually show the raw json, since it was getjson function, I did not know how to show the json but he showed me
Scarface
Uh. Just go to your JSON page directly...And download it or whatever. You don't need to do it in JS.
meder
lol there were too many undefined variables and complications, since I was retrieving information not posting it and retrieving, (it was not a form posting), which yes, I do admit that would have been easy. Mark had the right idea.
Scarface
A: 

What does your returned JSON look like? If you're returning an object, length might not be explicitly defined, whereas if you're returning an array, it should be defined automatically.

Matt Huggins
how can I show the raw json in this function?
Scarface
A: 

First thing that comes to mind is that length is not a property of whatever json is. What is the json variable supposed to be anyway?

George
A: 

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

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

Alternatively, grab a library like json2 and you'll be able to stringify the object for output/debugging.

thenduks
+1  A: 

pop the JSON in a span then clip it and paste it here so we can see it:

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

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

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

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

Mark Schultheiss
thanks for that mark, at the start, there is nothing because the function is designed to only return new information on a settimeout period. When information is sent, it looks like this for example [{"timestamp":"1279573958","user":"dfgdfgd","message":"t","creator":""}]
Scarface
Thus, there is no length. But, if you do alert(json[0].timestamp); you should see 1279573958 in the alert box.
Mark Schultheiss
yeah I knew there was no length, but it was somehow defined everytime except ofcourse on load when it would not be so I figured it was a javascript standard variable or something lol
Scarface
+2  A: 

Just check for the object being null or empty:

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

C'mon gang this was glaringly obvious :-)

Pointy
lol I think that may have fixed it, I will check back in a little bit after some testing
Scarface
ps I like your picture, classic painting
Scarface
@OK great! Yes it's an amazing painting, though I feel a little sorry for the poor girl. I always imagine her weeping while her slob of a dad yells,"You're marrying Mr Arnolfini, and **that** is **that**!"
Pointy
lol, I suppose that was a reality in those times. I am pretty sure the error is gone lol. Thanks again for your help.
Scarface