Ajax Chinese garbled solution (in progress)

Source: Internet
Author: User
Tags send cookies

Friends who have used Ajax certainly know that JavaScript is using UTF-8 International encoding, that is, each Chinese character with 4 bytes to store, but this caused the use of Ajax to send data when garbled.
One solution is to use encodeuricomponent and modify content-type to application/X-WWW-form-urlencoded to uniformly encode data into URL format. Of course, you can also specify encoding, for example, "application/X-WWW-form-urlencoded; charset = UTF-8", for example: http_request = new activexobject ("msxml2.xmlhttp"); http_request.setrequestheader ("Content-Type ", "Application/X-WWW-form-urlencoded; charset = UTF-8 ");
Another method is to convert the data into gb2312 format using functions written in VBScript. I personally think this method is better. If you are interested, you can go to the Internet to check it.

You can also use the escape Function to convert data to a hex string before JS sends data, and then convert it back to Unescape when reading data with Js.

Today, I was wondering, Will Ajax send cookies when calling them? I wroteProgramTest it. You can write the data to the cookie through javascript before calling Ajax, and then send the data to the cookie, great !!

Demo address: http://cn5.cn/ajax/ajax12.htm

ClientCodeAjax.htm

example of passing values using cookies in Ajax

function getready ()
{< br> If (XH. readystate = 4)
{< br> If (XH. status = 200)
{< br> odiv. innerhtml = "complete"
}< br> else
{< br> odiv. innerhtml = "sorry, data loading failed. Cause: "+ XH. statustext
}< BR >}// Author: longbill www.longbill.cn

function setcookie (name, value)
{< br> var cookiestr = Name + "=" + value + ";";
var expires = "";
var cookieexp = 60*60*1000;
var d = new date ();
D. settime (D. gettime () + cookieexp);
expires = "expires =" + D. togmtstring () + ";";
document. cookie = cookiestr + expires;
}< br> function $ (a)
{< br> return document. getelementbyid (a);
}< br>

<Body>
Example of passing values using cookies in Ajax: <br/>
<Form name = myform>
Name: <input id = Name value = "variable name or even Chinese" size = 20> <br/>
Value: <input type = text size = 20 id = Val value = here> <br/>
<Input onclick = "getxml ()" type = "button" value = "send data">
<Input onclick = "If (XH & XH. responsetext) {alert (XH. responsetext);} "type =" button "value =" show returned results "> <br/>
<Div id = m bgcolor = blue> display status here </div>
<Input type = button onclick = "alert (document. Cookie)" value = display local cookies>
</Form>

Server code A. php

<?
Header ("Content-Type: text/html; charset = gb2312 ");
Echo "The following are all COOKIE variables and their values \ n ";
Print_r ($ _ cookie );
?>

----------------

First, JavaScript follows the Java character processing method, and Unicode is used internally to process all characters,
Second, UTF-8 is stored in three bytes for each Chinese character (UNICODE character.
Third, garbled characters are not generated when UTF-8 is used to send data. garbled characters are generated only when the background program does not properly decode the data.
Fourth, when Ajax sends data, if the Content-Type is modified to application/X-WWW-form-urlencoded ", it must be post, however, "Too large data may often go wrong" is caused by the use of get to send data.
Fifth, the function written with VBScript is used to convert data into GBK encoding (the default encoding method of the operating system. If big5 is used in traditional Chinese systems, instead of gb2312, the number of characters in the two codes varies by about three times.
Sixth, use cookies to send data. First, it is easy to overflow, and second, it is necessary to keep wiping your ass. Otherwise, the data in the cookie is stored in each HTTP Request (including image and script requests) will be sent. Third, when several HTTP requests are concurrently sent, there is no way to specify the cookie to be sent to that HTTP request.

------------------------

When Ajax is used to get back to a page, most of the Chinese characters in responsetext will be garbled, this is because XMLHTTP in processing the returned responsetext, is the resposebody according to the UTF-8 encoding into the decoding test form, if the server sends a data stream that is indeed a UTF-8, the Chinese characters will be correctly displayed, and when the GBK encoding stream is sent out, it will be messy. The solution is to add a header in the sent stream to specify the encoding stream to be sent, so that XMLHTTP will not be messed up.

PHP: Header ('content-type: text/html; charset = gb2312 ');
ASP: Response. charset ("gb2312 ")
JSP: Response. setheader ("charset", "gb2312 ");

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.