<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[运维进行时]]></title> 
<link>https://blog.liuts.com/index.php</link> 
<description><![CDATA[互联网运维与架构]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[运维进行时]]></copyright>
<item>
<link>https://blog.liuts.com/post//</link>
<title><![CDATA[解决：Ajax utf-8中文乱码问题]]></title> 
<author>root &lt;admin@yourname.com&gt;</author>
<category><![CDATA[Ajax]]></category>
<pubDate>Thu, 22 May 2008 14:04:54 +0000</pubDate> 
<guid>https://blog.liuts.com/post//</guid> 
<description>
<![CDATA[ 
	<strong>中文字符转换成Utf8编码</strong><br/><textarea name="code" class="javascript" rows="15" cols="100">
function EncodeUtf8(s1)
&#123;
&nbsp;&nbsp;var s = escape(s1);
&nbsp;&nbsp;var sa = s.split("%");
&nbsp;&nbsp;var retV ="";
&nbsp;&nbsp;if(sa[0] != "")
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;retV = sa[0];
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;for(var i = 1; i < sa.length; i ++)
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;if(sa[i].substring(0,1) == "u")
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retV += Hex2Utf8(Str2Hex(sa[i].substring(1,5)));
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;else retV += "%" + sa[i];
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;return retV;
&#125;

function Str2Hex(s)
&#123;
&nbsp;&nbsp;var c = "";
&nbsp;&nbsp;var n;
&nbsp;&nbsp;var ss = "0123456789ABCDEF";
&nbsp;&nbsp;var digS = "";
&nbsp;&nbsp;for(var i = 0; i < s.length; i ++)
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;c = s.charAt(i);
&nbsp;&nbsp;&nbsp;&nbsp;n = ss.indexOf(c);
&nbsp;&nbsp;&nbsp;&nbsp;digS += Dec2Dig(eval(n));
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;return digS;
&#125;

function Dec2Dig(n1)
&#123;
&nbsp;&nbsp;var s = "";
&nbsp;&nbsp;var n2 = 0;
&nbsp;&nbsp;for(var i = 0; i < 4; i++)
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;n2 = Math.pow(2,3 - i);
&nbsp;&nbsp;&nbsp;&nbsp;if(n1 >= n2)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s += 1;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n1 = n1 - n2;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s += 0;

&nbsp;&nbsp;&#125;
&nbsp;&nbsp;return s;
&#125;

function Dig2Dec(s)
&#123;
&nbsp;&nbsp;var retV = 0;
&nbsp;&nbsp;if(s.length == 4)
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;for(var i = 0; i < 4; i ++)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retV += eval(s.charAt(i)) * Math.pow(2, 3 - i);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;return retV;
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;return -1;
&#125;

function Hex2Utf8(s)
&#123;
&nbsp;&nbsp;var retS = "";
&nbsp;&nbsp;var tempS = "";
&nbsp;&nbsp;var ss = "";
&nbsp;&nbsp;if(s.length == 16)
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;tempS = "1110" + s.substring(0, 4);
&nbsp;&nbsp;&nbsp;&nbsp;tempS += "10" + s.substring(4, 10);
&nbsp;&nbsp;&nbsp;&nbsp;tempS += "10" + s.substring(10,16);
&nbsp;&nbsp;&nbsp;&nbsp;var sss = "0123456789ABCDEF";
&nbsp;&nbsp;&nbsp;&nbsp;for(var i = 0; i < 3; i ++)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retS += "%";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ss = tempS.substring(i * 8, (eval(i)+1)*8);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retS += sss.charAt(Dig2Dec(ss.substring(0,4)));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retS += sss.charAt(Dig2Dec(ss.substring(4,8)));
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;return retS;
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;return "";
&#125;
</textarea><br/><strong>客户端</strong><br/>prototype框架，代码片断：<br/><textarea name="code" class="javascript" rows="15" cols="100">
var url = '/system/do/'+encodeURI(encodeURI(currvalue));
url=EncodeUtf8(url);
var myAjax = new Ajax.Request(
url,
&#123;
method: 'post',
onComplete: showResponse
&#125;);
</textarea><br/>注：<br/>1、此处一定要两次编码encodeURI(encodeURI<br/>2、然后将编码的结果转成UTF-8<br/>如不这样做，中文到服务器端将成“??????”的字符。<br/><strong>服务器端</strong><br/>$constr=urldecode(urldecode($constr));<br/>同样也需要进行两次解码：）<br/>Tags - <a href="https://blog.liuts.com/tags/%25E4%25B8%25AD%25E6%2596%2587%25E4%25B9%25B1%25E7%25A0%2581/" rel="tag">中文乱码</a>
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post//#blogcomment</link>
<title><![CDATA[[评论] 解决：Ajax utf-8中文乱码问题]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://blog.liuts.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>