javascript+php中文url编码|gb2312问题(ajax中文参数)

php页面文档为utf-8编码,html页面为gb2312。

第一种方法,使用encodeURIComponent一次转码:
"你好,world!"在html页面得出的编码为"%E4%BD%A0%E5%A5%BD%EF%BC%8Cworld!":
<html>
<head>
<script language="javascript">
a = encodeURIComponent('你好,world!');
window.location.href = a;
alert(a);
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<head>
<body>
你好
</body>
</html>
php页面转码,得出中文"你好,world!"(utf-8编码):
<?php
header("Content-type:text/html; charset=utf-8");
$string = '%E4%BD%A0%E5%A5%BD%EF%BC%8Cworld!';
$string = urldecode($string);
echo $string;
?>

第二种方法,使用encodeURIComponent两次转码:
"你好,world!"在html页面得出的编码为"%25E4%25BD%25A0%25E5%25A5%25BD%25EF%25BC%258Cworld!":
<html>
<head>
<script language="javascript">
a = encodeURIComponent(encodeURIComponent('你好,world!'));
window.location.href = a;
alert(a);
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<head>
<body>
你好
</body>
</html>
php页面转码,得出中文"你好,world!"(utf-8编码):
<?php
header("Content-type:text/html; charset=utf-8");
$string = '%25E4%25BD%25A0%25E5%25A5%25BD%25EF%25BC%258Cworld!';
$string = urldecode($string);
$string = iconv("UTF-8","GB2312",$string);
$string = urldecode($string);
echo $string;
?>
转自:http://hi.baidu.com/loveyoursmile/blog

你可能感兴趣的:(JavaScript,html,PHP,Ajax,Blog)