Don't include a "length" attribute in a JSON Object
Just spent a bunch of time debugging this one...
I was creating a JSON object in Javascript that I was then sending back to the server via AJAX. I was doing this to error-check user input on the server before submitting the form for real.
But I had a problem. Once of the attributes in my object was called "length" and anytime the user input for this parameter had text in it (instead of a number), the Javascript would mysteriously die. The check would completely fail and the back-end call to the server was never even made.
Turns out that if you have a JSON object and you call one of the parameters "length", this will override the usual length method for the object. And if this length parameter then contains something it shouldn't (like text) the processing of this object will get messed up and the code will barf all over you.
So the take-away message is, if you are creating a JSON object, never call one of the attributes in it "length". It'll mess you up... it just cost me over an hour.
I was creating a JSON object in Javascript that I was then sending back to the server via AJAX. I was doing this to error-check user input on the server before submitting the form for real.
But I had a problem. Once of the attributes in my object was called "length" and anytime the user input for this parameter had text in it (instead of a number), the Javascript would mysteriously die. The check would completely fail and the back-end call to the server was never even made.
Turns out that if you have a JSON object and you call one of the parameters "length", this will override the usual length method for the object. And if this length parameter then contains something it shouldn't (like text) the processing of this object will get messed up and the code will barf all over you.
So the take-away message is, if you are creating a JSON object, never call one of the attributes in it "length". It'll mess you up... it just cost me over an hour.
| Rating: | 100% positive, 1 Vote |
| Categories: | Javascript JSON programming debugging |
| Added: | on Apr 10, 2009 at 4:51 pm |
| Added By: | an anonymous user |
| Searches: | json object length javascript debugging |

