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:

JavaScript:
  1. 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...

CODE:
  1. pair has no properties
  2. http://projectname.localhost/js/prototype.js
  3. Line 417
  4.  
  5. [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...

JavaScript:
  1. if (Object.isString(this.options.parameters))
  2.     this.options.parameters = this.options.parameters.toQueryParams();
  3. else if (Object.isHash(this.options.parameters))
  4.     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!

JavaScript:
  1. 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.

4 Responses a “Parameters string and the Prototype Ajax.Request method”

  1. Feel free to submit any bugs/suggestions to prototype's bugtracker: http://prototype.lighthouseapp.com/projects/8886-prototype/tickets

    Ajax.Updater "defaults" its parameters to an empty object internally, so I'm not sure how this error could appear.

  2. Kangax: The copy of Prototype that i was using here, Prototype 1.6.0.2 defaults paramters to an empty string, so I'm not quite sure of where you're looking.

    Granted, null makes more sense than either option, but that's another point altogether.

    JavaScript:
    1. Ajax.Base = Class.create({
    2.   initialize: function(options) {
    3.     this.options = {
    4.       method:       'post',
    5.       asynchronous: true,
    6.       contentType:  'application/x-www-form-urlencoded',
    7.       encoding:     'UTF-8',
    8.       parameters:   '', // <-- empty string
    9.       evalJSON:     true,
    10.       evalJS:       true
    11.     };

  3. this is something else I think because

    var foo = '';
    console.log(foo.toQueryParams()); //empty object is returned
    So there is no problem with empty strings and toQueryParams().

  4. This may be related to this bug:
    http://prototype.lighthouseapp.com/projects/8886/tickets/169-ajax-request-fails-in-ie

    Not sure what is causing it though.
    - JDD

Leave a comment


Nater Kane naterkane personal http://www.naterkane.com LinkedIn Profile Web Technologist personal nater@naterkane.com 1978-09-12 voice 845.234.6698 | fax 707.922.0593
964 Flushing Ave. Brooklyn, NY 11206