21

Aug

If you use the Prototype JS library (like I do) in unison with the Javascript Prototype Model (like I do), you may eventually run into a conflict. For example, FireBug recently began spitting this little batch of nonsense at me when I tried to instantiate an Ajax.Request object:

[Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIXMLHttpRequest.setRequestHeader]” nsresult: “0×80070057 (NS_ERROR_ILLEGAL_VALUE)” location: “JS frame :: https://students.housing.wisc.edu/photos/common/lib/prototype.js :: anonymous :: line 1274″ data: no]

Long story short, the cause of this little headache is that Prototype JS will indiscriminately add headers to an XmlHttpRequest, regardless of their datatypes. Of course, if it’s trying to add, say, a function as a header, as it was in my case, there’s going to be a problem. Thankfully, I found an easy fix here.

Simply find the following code in your prototype.js file:


for (var name in headers)
this.transport.setRequestHeader(name, headers[name]);

And add a conditional:


for (var name in headers)
if (typeof(headers[name]) == "string")
this.transport.setRequestHeader(name, headers[name]);

It’s safe to narrow the headers to strings alone as, in my experience, they’ll never need to be any other datatype.

In reading up on this, I’ve found one school of thought that believes firmly that altering Object.prototype is a bad idea, but I disagree. It’s extremely useful in many cases. As far as I’m concerned, tinker away (especially since the core Javascript String functions are so spartan).


RSS Digg! Delicious StumbleUpon Newsvine Technorati ← Submit me!


Leave a Reply