Ajax just does a HTTP request to the URL you specify. On the server side, you handle it like any other web request.
I don't have the energy to write a custom example, i'm still suffering from an inflamed right wrist.
This example is just taken straight from my own framework (in this example, we are checking if the filename already exists on the server before an upload). First we need some JavaScript to define to do the Ajax call, see images.tt:
function ajaxUpdateFName() { if(alreadyUpdating) { return; } alreadyUpdating = 1; $( "#upbutton" ).button( "option", "disabled", true ); var origname_c = document.getElementById("upfile"); var newname_c = document.getElementById("upfname"); var description_c = document.getElementById("updescription"); var params = "fname=" + origname_c.value; srefresh.open("POST", ttvars.checkfname, true); //Send the proper header information along with the request srefresh.setRequestHeader("Content-type", "application/x-www-for +m-urlencoded"); srefresh.onreadystatechange=function() { if(srefresh.readyState == 4) { var serverresponse = srefresh.responseText; var parsed = JSON.parse(serverresponse); newname_c.value = parsed.fname; $("#ajaxwait").hide(); $("#upfnamestatus").html(parsed.statustext); if(parsed.description != '' && description_c.value == '') { description_c.value = parsed.description; } if(parsed.status == 'OK') { $("#upfnamestatus").css("color", "green"); $('#upfname').removeAttr("disabled"); $("#upbutton" ).button( "option", "disabled", false ); } else if(parsed.status == "WARNING") { $("#upfnamestatus").css("color", "orange"); $('#upfname').removeAttr("disabled"); $("#upbutton" ).button( "option", "disabled", false ); } else { $("#upfnamestatus").css("color", "red"); } $("#upfnamestatus").show(); alreadyUpdating = 1; } } srefresh.send(params); }
On the Perl side, we then handle the request and send a document to the client, see Images.pm:
sub get_fname($self, $ua) { my $dbh = $self->{server}->{modules}->{$self->{db}}; my $filename = $ua->{postparams}->{'fname'} || ''; $filename = $self->clean_fname($filename); my %data = ( fname => $filename, status => 'OK', statustext => 'OK', description => '', ); # Check for existing filename my $selsth = $dbh->prepare_cached("SELECT * FROM " . $self->{tablename} . " WHERE filename = ? LIMIT 1") or croak($dbh->errstr); $selsth->execute($filename) or croak($dbh->errstr); my $existcount = 0; while((my $line = $selsth->fetchrow_hashref)) { $existcount++; $data{description} = $line->{description}; } if($existcount) { $data{status} = 'WARNING'; $data{statustext} = "File exists, will overwrite!<br/><b>This +may lead to caching problems for the next few hours!</b>"; } my $jsondata = encode_json \%data; return (status => 200, type => "text/plain", data => $jsondata); }
The code in your project will probably look quite a bit different (different framework, different task), but the gist of it will be the same:
Hope that helps.
In reply to Re^4: how to make dependent drop down in perl script
by cavac
in thread how to make dependent drop down in perl script
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |