Re: AJAX and Perl-based web applications
by redhotpenguin (Deacon) on Jun 25, 2005 at 02:13 UTC
|
Here's how I built my first AJAX widget using Template, Javascript, and CGI::Application. It removes usernames from a list when the user clicks the remove link, without refreshing the page. The hardest part was figuring out how to properly POST using the XmlHttpRequest object. Note that my CGI handler does not return anything in this particular case.
In the template code:
<script type="text/javascript">
var xmlhttp=false;
/*@cc_on @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
function myfunction(username) {
var params = "rm=remove&username="+username;
var objectlist = document.getElementById(objectlist);
// rm is the node we want to remove from the DOM
var rm = document.getElementById(username);
// The true attribute means the script continues after
// the post has been executed
xmlhttp.open("POST", '/my/runmode/', true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-u
+rlencoded");
xmlhttp.setRequestHeader("Connection","close");
// Connection is to be closed after transfer
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
// Delete the html node when request is complete
rm.parentNode.removeChild(rm);
}
}
xmlhttp.send(params);
}
</script>
<ul id="objectlist">
[% FOREACH object = objects %]
<p id="[% object.username %]">[% object.username %] -
<a href="#"
onclick="myfunction('[% object.username %]');">
remove</a>
</p>
[% END %]
</ul>
Meanwhile in my CGI::App handler...
sub setup {
my $self = shift;
$self->start_mode('default');
$self->run_modes( [ qw( default remove ) ] );
}
sub remove {
my $self = shift;
my $user =
My::User->retrieve(
username => $self->query->param('username')
);
$user->delete;
}
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: AJAX and Perl-based web applications
by etcshadow (Priest) on Jun 25, 2005 at 01:44 UTC
|
I've been writing "AJAX" applications in perl since well before it had the name AJAX. i haven't really used any special tools.
There exist some modules on CPAN for JSON conversion into and out of perl, but a) JSON doesn't fully work on older browsers (like IE's older than 6), and b) we'd written a version (which produces much uglier javascript that works on older versions of IE) well before JSON became a buzzword, itself.
I don't know what to tell you, exactly... some folks might be working on some clever tools, somewhere, that present a useful abstraction on AJAX, but I know that I (and my team) just deal with the complexities of it. Granted we've built some tools (you know, some simple javascript libraries to manage the synchronous and the asynchronous http requests and such). But these are just tools to make the task easier, nothing that really abstracts away the fact that there are these requests coming and going, triggered by DHTML events and so on. I don't know if anyone else has.
------------
:Wq
Not an editor command: Wq
| [reply] [Watch: Dir/Any] [d/l] |
Re: AJAX and Perl-based web applications
by cees (Curate) on Jun 25, 2005 at 13:29 UTC
|
There really isn't much difference to building a script that handles an AJAX request compared to a regular request. The main difference is that the AJAX request usually returns XML, or possibly just a small snippet of HTML.
The hard part around AJAX is the JavaScript side (since I am a perl developer, JavaScript is hard ;). There are already some JavaScript libraries out there that can really simplify this. I've used one called Sarissa that works pretty well, but I haven't done enough in this area to recommend the "best" JavaScript library out there. Just do some searching.
If you want an example using CGI::Application, then you can have a look at my attempt at building an Upload Meter (something like Apache::UploadMeter). It is just a prototype, so there isn't much in the way of documentation, but it does show how you can do AJAX with CGI::Application (as you will notice, it is no different from a regular CGI::Application module). Here is a tarball of the entire prototype.
I would like to wrap this up in a simple to use module at some point, but time is my enemy right now :)
| [reply] [Watch: Dir/Any] |
Re: AJAX and Perl-based web applications
by dragonchild (Archbishop) on Jun 27, 2005 at 02:55 UTC
|
I'm doing R&D for my employer using Catalyst for AJAX applications right now. The key is the Javascript library you use. Remember - Ajax is just a way for the browser to make HTTP requests without having to refresh the page. Nothing less, nothing more. On the serverside, you're doing standard HTTP request/responses. You can use Catalyst, CGI::Application, Maypole, OpenInteract, or whatever else you want to use.
The JS side is where things get interesting. I personally use HTML::Prototype and the Prototype JavaScript library for my AJAX work. (I have documentation for this forthcoming.)
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
| [reply] [Watch: Dir/Any] |
Re: AJAX and Perl-based web applications
by sri (Vicar) on Jun 25, 2005 at 05:01 UTC
|
I hope Catalyst "is" a standard Perl web tool.
Note that you can run Catalyst apps as cgi too. (next to mod_perl1/1.99/2,fcgi...yadayada ) | [reply] [Watch: Dir/Any] |
|
Yep, I would say it is. I didn't phrase the question quite right--I just meant that I was looking for a general answer to what people were doing to apply AJAX-type thinking to their perl-based CGI applications.
| [reply] [Watch: Dir/Any] |
Re: AJAX and Perl-based web applications
by Anonymous Monk on Jun 25, 2005 at 04:42 UTC
|
| [reply] [Watch: Dir/Any] |
Re: AJAX and Perl-based web applications
by pinguxx (Initiate) on May 26, 2006 at 19:30 UTC
|
have u tried Perl-Ajax??
it works ok for me | [reply] [Watch: Dir/Any] |
|
I have tried the perl AJAX Module but I am not getting successful in this and finding it very complex.
I have tried the example given on link :
http://www.perl.com/pub/a/2006/03/02/ajax_and_perl.html?page=2
But it is not working completely.
It would be a great help if you could provide some working easy example to understand the basics of CGI::AJAX.
Many thanks
-Vivek
| [reply] [Watch: Dir/Any] |