Ajax implementation in Perl and Chinese problems

Source: Internet
Author: User
Tags character set header return
ajax|perl| Questions | Chinese

Ajax, the latest very booming technology. There are a lot of ready-made development kits. At the beginning I tried the cpaint and supported php/asp. Not bad. It was later started with Perl Ajax: Cgi::ajax. By contrast, the discovery was very useful. The main feature is that the program automatically generates code that JavaScript invokes.

In this way, we can focus on the development of program logic without having to ignore the cumbersome Ajax JavaScript calls. This is the place where the cgi::ajax is clever.

Cgi::ajax Installation

Perl-mcpan-e "Install Cgi::ajax"

Learn about this simple example

#!/usr/bin/perl-w use strict; Use Cgi::ajax; Use CGI; #下面是我们这个程序的核心逻辑部分. #客户端通过会Ajax调用这个函数, get the results you want. Sub test_calculate{I ($cal 1, $cal 2) =@_ return ($cal 1+ $cal 2); #下面是客户端显示的html #注意onclick部分的写法. You can interpret it as a piece of pseudo code (in fact, it is not a pseudo code, its actual code is cgi::ajax automatically generated), said: #调用函数calculate, parameters are Val1 controls and val2 control values, the results are displayed on the result control. Sub show_html{My $html = <<EOHTML; <HTML> <HEAD> <title>test</title> </HEAD> < body> <input type= ' text ' name= ' val1 ' id= ' val1 ' > <input type= ' text ' name= ' val2 ' id= ' val2 ' > <input type= ' button ' Name= ' cal ' id= ' cal ' value= ' cal it ' ><br> <div id= ' result ' ></div> </BODY>

A little problem

If you need to use a carriage return character or other similar characters in a page's JavaScript, you must use a 2 slash.

Questions about Chinese

I take UTF8 as an example.

1. page Character Set setting

Use the following calling method.

My @headers = ("Charset=utf8",); Print $ajax->build_html ($cgi, \&show_html,\ @headers);

2. Return result set Chinese display garbled

This is a small bug in Cgi::ajax.

The solution is as follows:

Find the ajax.pm you installed and find the following sentence:

My $rv = $self->cgi ()->header ();

Change it to a sentence similar to this:

My $rv = $self->cgi ()->header ("Charset=utf8");

Of course, the better way is to use our

Print $ajax->build_html ($cgi, \&show_html,\ @headers); This @headers, which is 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.