Ajax, a very popular technology recently. There are many ready-made development kits. At the beginning, I tried CPAINT and supported php/asp. Not bad. Later, we started to use perl's Ajax implementation: CGI: Ajax. In comparison, it is found that it is very useful. The main feature is that the program automatically generates javascript call code. In this way, we can focus on the development of program logic without ignoring complicated Ajax javascript calls. This is where CGI: Ajax is brilliant.
CGI: Ajax Installation
Perl-MCPAN-e "install CGI: Ajax"
Learn this simple example
#! /Usr/bin/perl-w use strict; use CGI: Ajax; use CGI; # below is the core logic of our program. # The client calls this function through Ajax to obtain the desired result. Sub test_calculate {my ($ cal1, $ cal2) =_; return ($ cal1 + $ cal2) ;}# The following is the html displayed on the Client # Note The onclick part. You can understand it as a pseudo code that is not actually a pseudo code. The actual code is generated automatically by CGI: Ajax.) # Call the calculate function, parameters are the values of the val1 and val2 controls, and the results are displayed on the result control. Sub Show_HTML {my $ html = < Test
EOHTML return $ html;} my $ cgi = new CGI (); # The code here associates the pseudo code calculate just mentioned with our perl implementation function. # In this way, when the client clicks the calculate pseudo code, the effect is: # The javascript code automatically generated by the program will start the Ajax mechanism and remotely call the test_calculate function in our cgi, # obtain the result and output it to the corresponding page control for display. My $ ajax = new CGI: Ajax ('calculate' => \ & test_calculate); # build_html, In this step, CGI :: ajax automatically generates all javascript code called by html webpages and Ajax. # When the client calls this program through Ajax, The build_html function automatically calls the corresponding function test_calculate) and returns the result. Print $ ajax-> build_html ($ cgi, \ & Show_HTML );
A small problem
To use carriage return or other similar characters in javascript on the page, you must use two slashes.
Chinese problems
Take utf8 as an example.
1. Page Character Set
Use the following call method.
My @ headers = ("charset = utf8",); print $ ajax-> build_html ($ cgi, \ & Show_HTML, \ @ headers );
2. Garbled text in the returned results
This is a small Bug in CGI: Ajax.
The solution is as follows:
Find the installed Ajax. pm and find the following sentence:
My $ rv = $ self-> cgi ()-> header ();
Change it to a sentence like this:
My $ rv = $ self-> cgi ()-> header ("charset = utf8 ");
Of course, a better way is to use
Print $ ajax-> build_html ($ cgi, \ & Show_HTML, \ @ headers); the @ headers passed in is also easy to change.