Ajax Chinese garbled Solution final version-compatible IE and Ff__ajax

Source: Internet
Author: User

The core of Ajax technology is JavaScript, and JavaScript uses UTF-8 encoding, so when the page uses GBK or other encoding, without encoding conversion, there will be the problem of garbled Chinese. Here are the different test results and garbled solutions for the page using GBK and UTF-8 encoded under IE and FF, respectively, with Get and post methods

Pass Value method Client-side encoding Server-side encoding Ie Ff Solution
Get UTF-8 UTF-8 Garbled when receiving parameters passed by $_get Normal Client Url=encodeuri (URL)
Get GBK GBK Normal Garbled when receiving parameters passed by $_get Client Url=encodeuri (URL)
Server-side
$str =iconv ("UTF-8", "GBK", $str)
POST UTF-8 UTF-8 Garbled when receiving parameters passed by $_get Normal Client Url=encodeuri (URL)
POST
UTF-8 UTF-8 Normal to receive parameters passed by $_post Normal to receive parameters passed by $_post Recommended way to use
POST GBK GBK Normal Garbled when receiving parameters passed by $_get Client Url=encodeuri (URL)
Server-side
$str =iconv ("UTF-8", "GBK", $str)
POST GBK GBK Garbled when receiving parameters passed by $_post Garbled when receiving parameters passed by $_post Server-side
$str =iconv ("UTF-8", "GBK", $str)

The following is the code for the test:

Client index.php

<?php header ("Content-type:text/html;charset=utf-8");?> <input type= "button" value= "Load" id= "Button1" >
		<div id= "Div1" >Loading</div> <script type= "Text/javascript" > Window.onload=function () {
			document.getElementById ("Button1"). Onclick=function () {var xmlHttp;
			var url= "t.php?id= ' Test ' &nu=" +math.random ();              Url=encodeuri (URL); Here is the key if (window).
			XMLHttpRequest) {xmlhttp=new XMLHttpRequest ();
			else {xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function () {if (xmlhttp.readystate==4&&xmlhttp.status==200) {document.ge
				Telementbyid ("Div1"). Innerhtml=xmlhttp.responsetext;
			} xmlhttp.open ("Get", url,true);
			Xmlhttp.send ();
			Xmlhttp.open ("POST", "t.php", true);
			Xmlhttp.open ("POST", url,true);
			Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
			Xmlhttp.send ("id= ' Test ' &nu=" +math.random ());
Xmlhttp.send ();		}} </script> 

Server-Side t.php

<?php
	Header ("Content-type:text/html;charset=utf-8");
	echo "This is Test Chinese"; If this is directly to the client output Chinese, not through the $_get or $_post pass parameters
	//Only the client and server-side coding consistent, whether UTF-8 or GBK, will not appear garbled
	echo $_get[' id '].$_get[ ' Nu '];
	Echo iconv ("UTF-8", "GBK", $_get[' id '].$_get[' nu ']);
	echo $_post[' id '].$_post[' nu '];
	Echo iconv ("UTF-8", "GBK", $_post[' id '].$_post[' nu ']);
? >

Another problem may exist in IE:

The operation could not be completed due to an error c00ce56e

At this point, the header (' Content-type:text/html;charset=utf8 ') is changed to header (' Content-type:text/html;charset=utf-8 ') when the encoding is set.

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.