Jquery + ASPajax solves Chinese garbled characters

Source: Internet
Author: User
When creating a website, the Simplified Chinese version of gb2312 is set as the website code because the website was not designed so much. It is a normal event to use gb2312 Encoding As a Chinese character, and there is no major problem in building a website, including compiling JSAjax by yourself. As Jquery becomes popular, I gradually like Jquery to operate D.

When creating a website, the Simplified Chinese version of gb2312 is set as the website code because the website was not designed so much.

It is a normal event to use gb2312 Encoding As a Chinese character, and there is no major problem in website construction, including compiling JS Ajax by yourself.

With the popularity of Jquery, I gradually liked Jquery's method of DOM operations. Later, I used Jquery files as a public JS library for the whole site.

Jquery's Ajax is also quite useful, because it is a public file, so you don't have to write any XMLHTTPrequest objects for every page that requires Ajax, so you start to use Jquery's Ajax.

Some problems are found during use:

The first problem is load, which is easier to cache than Get, that is, the output content is the same every time,

This is a good solution. Simply add a random number after the request URL, so that the URL is different and will not be cached.

The second problem is the garbled problem, which is quite time-consuming. After a while, I have come up with two solutions and record them to avoid forgetting them.

Method 1:

Encode the requested page as a UTF-8 file and set <% @ LANGUAGE = "VBSCRIPT" CODEPAGE = "65001" %> the server encoding to 65001 to indicate UTF-8, finally, the output code on the page is also set to UTF-8, that is, response. charset = "UTF-8", such as encoding, can solve the garbled Problem

<% @ LANGUAGE = "VBSCRIPT" CODEPAGE = "65001" %>
<%
Response. Charset = "UTF-8"
%>

Note that the page must be saved as a UTF-8 file. Otherwise, the whole page will be garbled!


Method 2:

The second method is to modify the jquery file!

I use Version 1.3.2. This is an example!

One of the most important methods for Jquery to handle ajax is ajax: function (s ){}

Whether it is $. load, $. get, $. post is finally processed in this method. No matter what else, you can directly find it. Why does Jquery convert all our passed parameters into UTF-8 encoded data,

The data parameter passed by jquery ajax is finally passed to the param: function (a) {} method for processing,

Jquery param is written as follows:


Param: function (a) {var s = [];
Function add (key, value ){
S [s. length] = encodeURIComponent (key) + '=' + encodeURIComponent (value );
};
// If an array was passed in, assume that it is an array
// Of form elements
If (jQuery. isArray (a) | a. jquery)
// Serialize the form elements
JQuery. each (a, function (){
Add (this. name, this. value );
});
... Is not stuck.


You can see the code s [s. length] = encodeURIComponent (key) + '=' + encodeURIComponent (value.

This is the reason for garbled characters, because the encodeURIComponent method is to encode the characters according to UTF-8 encoding and URI components (you may not understand it)

Let's improve this so that he can also perform URI Encoding Based on gb2312.

Var CharsetUtf = true;
Try {if (a. ParamCharset | a. ParamCharset = "gb2312") {CharsetUtf = false ;}} catch (e ){};
Function add (key, value ){
If (CharsetUtf)
{S [s. length] = encodeURIComponent (key) + '=' + encodeURIComponent (value );}
Else
{S [s. length] = escape (key) + '=' + escape (value );}
};

This is a simple modification. The principle is to define a variable to determine which character encoding is used for URI encoding.

If it is true, press UTF-8. If it is fales, press gb2312;

The default value is true. UTF-8 is used for processing!

Let's make a judgment!

Try {if (a. ParamCharset | a. ParamCharset = "gb2312") {CharsetUtf = false ;}} catch (e ){};

The code indicates whether the passed data a has the ParamCharset attribute. If yes, it determines whether it is equal to "gb2312". If yes, change CharsetUtf to false;

If no, it is not processed.

Try catch: This prevents data errors from being processed in UTF-8 format. To prevent attacks.


Well, I have written so much. Now let's call it and try:


$. Post ("/zhanHui/ajaxAsp/SearchPageZuiXin. asp"
, {SPageSize: pPageSize, page: pPage, ajaxContent: pAjaxContent, ParamCharset: "gb2312 "}
, Function (data ){
}, "Json ");

According to the above Code, as long as we add a ParamCharset: "gb2312" to the data parameter during the call"

This parameter is passed into the data of gb2312. It is useless to pass this parameter, that is, it is passed into a UTF-8 data.


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.