Adding unique ID to AJAX request / response objects

1,111 views
Skip to first unread message

emwebdev

unread,
Sep 23, 2008, 11:43:27 PM9/23/08
to Prototype & script.aculo.us
Hi there

I need a way to uniquely identify a particular AJAX request / response
cycle through some kind of shared unique ID or similar.

The reason for this is that I have an AJAX "status" display on a page
which keeps a history of AJAX requests and their success / failure
results, but at the moment I am assuming that so long as the URL is
the same, then the response must be related. This works OK, but if
there are two AJAX requests to the same URL everything goes out the
window.

I thought it must be possible to extend the Prototype Ajax object
somehow to achieve this, but I'm having no luck.

Can anyone suggest the best way to make this work?


Cheers,

Erin

bluezehn

unread,
Sep 24, 2008, 7:56:09 AM9/24/08
to Prototype & script.aculo.us
from within a class, you can use class.constructor to refer to the
class rather than an instance of it. so you can have the equivalent of
a "static" array or something on the class.

initialize: function()
{
.......
this.register();
}

register: function()
{
this.constructor.instances.push(this.detailsYouWantToSave);
this.id = this.constructor.instances.length;
}

or something like that

Eric

unread,
Sep 24, 2008, 8:22:24 AM9/24/08
to Prototype & script.aculo.us
On Sep 24, 5:43 am, emwebdev <emweb...@gmail.com> wrote:
> Hi there

Hi!

> I need a way to uniquely identify a particular AJAX request / response
> cycle through some kind of shared unique ID or similar.
>
> The reason for this is that I have an AJAX "status" display on a page
> which keeps a history of AJAX requests and their success / failure
> results, but at the moment I am assuming that so long as the URL is
> the same, then the response must be related. This works OK, but if
> there are two AJAX requests to the same URL everything goes out the
> window.

Why don't you just add a dummy identifier to yor AJAX URL.
Something like using "myajax.php?ID="+Date.now()

So all your URL will be different, and your current solution will work
fine.

Of course, if you want to use this issue to learn more about extending
classes, you may use a more complex approach :o)

Eric

ColinFine

unread,
Sep 24, 2008, 8:32:50 AM9/24/08
to Prototype & script.aculo.us


On Sep 24, 1:22 pm, Eric <LeFauv...@gmail.com> wrote:
> On Sep 24, 5:43 am, emwebdev <emweb...@gmail.com> wrote:
>
> > Hi there
>
> Hi!
>
> > I need a way to uniquely identify a particular AJAX request / response
> > cycle through some kind of shared unique ID or similar.
>
> > The reason for this is that I have an AJAX "status" display on a page
> > which keeps a history of AJAX requests and their success / failure
> > results, but at the moment I am assuming that so long as the URL is
> > the same, then the response must be related. This works OK, but if
> > there are two AJAX requests to the same URL everything goes out the
> > window.
>
> Why don't you just add a dummy identifier to yor AJAX URL.
> Something like using "myajax.php?ID="+Date.now()
>
I do something like this, but use a (global) counter that I simply
increment each time for the ID. I then use the ID in the server code
to make sure that any DOM elements I generate have a unique ID (that
the code knows about in advance).

Colin

emwebdev

unread,
Sep 28, 2008, 10:57:12 PM9/28/08
to Prototype & script.aculo.us
Thanks to everyone for their help.

In the end, the following code sufficed for my needs:

var uid = function()
{
var id = 0;

return function()
{
return id ++;
}
}();

Ajax.Responders.register
({
onCreate: function(request)
{
request.uid = uid();
}
});

This made the UID available through request.uid, and additionally the
response object contains a reference to the original request object
also, so you can use response.request.uid to retrieve the same value.
Reply all
Reply to author
Forward
0 new messages