MooTools Request.JSON “success” event – Some Undocumented Behavior

Request.JSON is a simple extension of the Request class that MooTools provides for making HTTP request. According to the MooTools documentation for the Request.JSON class the handler for the “success” event will receive two arguments. The first one being the response JSON object, and the second, the response in text form. The signature of the handler function would then be something like this:

OnSuccess(responseJSON, responseText)

Now when a request is made without any arguments, like below

var req = new Request.JSON(
{
    method: method,
    url: url
});

req.addEvent("failure", failurefn);
req.addEvent("success", successfn);
req.send();

that is indeed the case. The success function gets the responseJSON and the responseText.

However if the request includes arguments, like so:

var req = new Request.JSON(
{
    method: method,
    url: url
});

req.addEvent("failure", failurefn);
req.addEvent("success", successfn);
req.send(args);

the 2 arguments to the success function are first the response JSON object and second the arguments (args) that were originally passed into the request. In other words the signature of the success function is like this:

OnSuccess(responseJSON, args)

I couldn’t find any mention of this in the MooTools documentation, and I think this might be a behavior of the underlying XmlHttpRequest object itself and not something introduced by the MooTools class.

Digg This
Advertisements

About floatingfrisbee

A programmer/blogger from New York City
This entry was posted in JavaScript, Mootools and tagged , . Bookmark the permalink.

4 Responses to MooTools Request.JSON “success” event – Some Undocumented Behavior

  1. Roark says:

    Hey,

    Great Post, thanks.

    Was wondering if you know why request.JSON would fail in IE and firefox?

    Or how would I handle the error? is there any way of telling why it failed?

    Thanks
    Roark

  2. Roark says:

    Sorry,

    Fail in IE and NOT Firefox

  3. Grace says:

    Hi Jaspreet,

    I finally found out why you could not get ResponseText from the arguments of your callback function. I think there is nothing wrong with Mootools’ Request class, but the bindWithEvent is buggy.

    I guess your successfn should be like “somefuntion.bindWithEvent(this, args)”. In Mootools 1.2.x, if the number of arguments passed along with an event are more than 1, you will only get the first argument in your callback function.

    In your case, the request success event fires [ResponseJSON, ResponseText], and bindWithEvent wraps with args, so in your callback function, you will get only [ResponseJSON, args].

    Anyhow, bindWithEvent is not in Mootools 1.3 anymore.

    Cheers!
    Grace

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s