Ajax Implementation of Perl and Chinese problems

Source: Internet
Author: User

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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.