#!/usr/bin/perl =head1 NAME ajax - Show a sample ajax request =cut use strict; use warnings; use base qw(CGI::Ex::App); use CGI::Ex::Dump qw(debug); use CGI::Ex::JSONDump qw(JSONDump); __PACKAGE__->navigate; ###----------------------------------------------------------------### sub main_hash_swap { return { colors => [qw(Red Green Blue Yellow Orange Purple)], }; } sub main_finalize { my $self = shift; my $form = $self->form; debug "You sent the following form", $form; return 0; } sub main_file_print { return \ q{
}; } ###----------------------------------------------------------------### sub js_colors_run_step { my $self = shift; my $color = $self->form->{'color'}; my @colors; @colors = map { "$color $_" } qw(Circle Square Triangle Rectangle Pentagon); $self->cgix->print_content_type('application/x-javascript'); print JSONDump(\@colors, {pretty => 1}); return 1; } ###----------------------------------------------------------------### sub ajax_js_run_step { shift->cgix->print_content_type('application/x-javascript'); print q { /* ajax.js - Paul Seamons - License BSD - Copyright 2007 */ /* $Id: ajax.js,v 1.1 2007-02-23 21:36:20 paul Exp $ */ var ajax_activexobject_types = [ "Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]; function ajax_xmlhttp_request () { var req; if (window.XMLHttpRequest) { try { req = new XMLHttpRequest() } catch (e) { } } else if (window.ActiveXObject) { for (var i = 0; i < ajax_activexobject_types.length; i++) { try { req = new ActiveXObject(ajax_activexobject_types[i]) } catch(e) { } if (req) { ajax_activexobject_types = [ajax_activexobject_types[i]]; // cache break; } } } return req; } function ajax_request (args) { var req = args.request ? args.request : ajax_xmlhttp_request(); if (! req) { alert('Could not find a workable XMLHttpRequest object.'); return } if (! args.method) args.method = 'GET'; if (! args.url) { alert('Missing url parameter'); return } if (typeof(args.async) == 'undefined') args.async = true; if (! args.content) args.content = ''; if (! args.handler) { alert('Missing handler function parameter'); return } if (! args.username) req.open(args.method, args.url, args.async); else req.open(args.method, args.url, args.async, args.username, args.password); req.onreadystatechange = function () { if (req.readyState != 4) { if (args.handler_other_state) args.handler_other_state(req, args, req.readyState); return; } if (req.status != 200) { if (req.status == 0) { if (args.handler_0_status) args.handler_0_status(req, args); return; } if (args.handler_other_status) args.handler_other_status(req, args, req.status); alert('Got a non-200 status: '+req.status+' - '+req.statusText); return; } args.handler(req, args); }; if (args.headers) for (var i = 0; i < args.headers.length; i++) req.setRequestHeader(args.headers[i][0], args.headers[i][1]); req.send(args.content); return req; } }; }