当前位置:Gxlcms > PHP教程 > ajax如何传中文参数乱码解决办法_PHP教程

ajax如何传中文参数乱码解决办法_PHP教程

时间:2021-07-01 10:21:17 帮助过:2人阅读

ajax如何传中文参数乱码解决办法 由于某种原因ajax默认就是uft-8的编码,那么我们使用一gbk就容易出现乱码的问题,下面我们就来告诉你php ajax乱码的解决方法吧。

ajax如何传中文参数乱码解决办法
由于某种原因ajax默认就是uft-8的编码,那么我们使用一gbk就容易出现乱码的问题,下面我们就来告诉你php教程 ajax乱码的解决方法吧。
传递中文参数,然后修改数据库教程的。


参数是中文encodeuricomponent这个方法必须调用两次
*/

function utf8rawurldecode ($source) {
$decodedstr = "";
$pos = 0;
$len = strlen ($source);
while ($pos < $len) {
$charat = substr ($source, $pos, 1);
if ($charat == '%') {
$pos++;
$charat = substr ($source, $pos, 1);
if ($charat == 'u') {
// we got a unicode character
$pos++;
$unicodehexval = substr ($source, $pos, 4);
$unicode = hexdec ($unicodehexval);
$entity = "&#". $unicode . ';';
$decodedstr .= utf8_encode ($entity);
$pos += 4;
}
else {
// we have an escaped ascii character
$hexval = substr ($source, $pos, 2);
$decodedstr .= chr (hexdec ($hexval));
$pos += 2;
}
} else {
$decodedstr .= $charat;
$pos++;
}
}
return $decodedstr;
}


/*
注:js中,在使用字符转码的时候,推荐使用 encodeuricomponent() 或者 encodeuri(),而非escape()。原因在于,escape() 只是为 ascii字符 做转换工作,转换成的 %unnnn 这样的码,如果要用更多的字符如 utf-8字符库就一定要用 encodeuricomponent() 或 encodeuri() 转换才可以成 %nn%nn 这的码才可以。

js:encodeuricomponent —— decodeuricomponent;php: rawurlencode —— rawurldecode


*/

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/632362.htmlTechArticleajax如何传中文参数乱码解决办法由于某种原因ajax默认就是uft-8的编码,那么我们使用一gbk就容易出现乱码的问题,下面我们就来告诉你php...

人气教程排行