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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值