Parameters string and the Prototype Ajax.Request method
I just noticed something that's a little funny about Prototype 1.6>. I've successfully deployed a number of projects with v1.5 of the library, with all of the improvements I've been reading about with v1.6, I decided to keep up with the times and use the newest version for a project that is very DOM Scripting intensive.
While making a request with either new Ajax.Request or new Ajax.Updater something kept going wrong.
Though the Prototype API Docs were giving examples such as this:
-
new Ajax.Updater('products', '/some_url', { method: 'get' });
Firebug was throwing me an error. Hooray for Firebug for doing it's job. Well, that's what I thought at first, until it was time to see what this error was about. Long story short, Google returned nothing...
-
pair has no properties
-
http://projectname.localhost/js/prototype.js
-
Line 417
-
-
[Break on this error] if ((pair = pair.split('='))[0]) {
It turns out that this error was being thrown by Prototype's toQueryParams method. toQueryParams was being called by Ajax.Base's initialize method...
-
if (Object.isString(this.options.parameters))
-
this.options.parameters = this.options.parameters.toQueryParams();
-
else if (Object.isHash(this.options.parameters))
-
this.options.parameters = this.options.parameters.toObject();
I said to myself (in the third person) "Nater, you're not passing any params, the request URL is specified elsewhere, AND it's 'friendly' anyway... this seems silly". Looking up a couple of lines it turns out that the default Ajax.Base.options.parameters key is set to an empty string by default. Since toQueryParams() is not happy when it's passed an empty (albeit default) string, what else can we do? The simple answer is to pass an empty object instead!
-
new Ajax.Updater('products', '/some_url', { method: 'get' ,parameters:{}});
Voila! Since this seems like it's something that's way too easy to come across, I haven't yet figured out if it's a bug or I'm just missing something that's even more basic.

