<?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/139/</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/139/</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/106/</link>
<title><![CDATA[在prototype中遍历表单对象]]></title> 
<author>root &lt;admin@yourname.com&gt;</author>
<category><![CDATA[Ajax]]></category>
<pubDate>Thu, 22 May 2008 10:00:18 +0000</pubDate> 
<guid>https://blog.liuts.com/post/106/</guid> 
<description>
<![CDATA[ 
	在使用prototype的Updater方法提交一个表单时，需要遍历每个表单项的值，一个个给URL变量赋值很麻烦，方法如下：<br/><textarea name="code" class="javascript" rows="15" cols="100">
<DIV class=quote>function userinfo_do()
&#123;
 var url = '/center/edit';
 pars+=Form.serialize($(editform));
 var myAjax = new Ajax.Updater(
 'mainpage',
 url,
 &#123;
&nbsp;&nbsp;method: 'post',
&nbsp;&nbsp;parameters: pars,
&nbsp;&nbsp;evalScripts: true
 &#125;);
&#125;</DIV>
</textarea><br/>使用Form.serialize($(editform))，editform为表单名。<br/>Tags - <a href="https://blog.liuts.com/tags/prototype/" rel="tag">prototype</a>
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post/105/</link>
<title><![CDATA[使用prototype的Ajax.Updater更新带javascript代码]]></title> 
<author>root &lt;admin@yourname.com&gt;</author>
<category><![CDATA[Ajax]]></category>
<pubDate>Thu, 22 May 2008 09:52:56 +0000</pubDate> 
<guid>https://blog.liuts.com/post/105/</guid> 
<description>
<![CDATA[ 
	prototype的Ajax.Updater可以用来更新一段html，但在更新带javascrīpt的html时，需要注意几个问题<br/>一. 需要在Ajax.Updater添加参数 evalscrīpts : true<br/>二. javascript代码块要使用下面的形式<br/><textarea name="code" class="javascript" rows="15" cols="100">
showHi = function() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;alert('Hi');
&#125;
</textarea><br/>而不能使用下面的形式<br/><textarea name="code" class="javascript" rows="15" cols="100">
function showHi() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;alert('Hi');
&#125;
</textarea><br/>三. 注意javascript代码块中不能使用 <!--&nbsp;&nbsp;//--> 标记，否则 IE 会出错，firefox没问题。<br/>下面是个例子, 在a1.jsp中加载a2.jsp：<br/>a1.jsp代码<br/><textarea name="code" class="html" rows="15" cols="100">
<html>
 <head>
 <title>Insert title here</title>
 <script type="text/javascript" src="prototype-150.js"></script>
 <script type="text/javascript">
 function ajax() &#123;
&nbsp;&nbsp;var url = "/MyWebApp/a2.jsp";
 var myajax = new Ajax.Updater(
 &#123;success: 'mydiv'&#125;,
 url,
 &#123;
&nbsp;&nbsp;asynchronous:true,
&nbsp;&nbsp;method: 'get',
&nbsp;&nbsp;evalscripts:true
 &#125;
 );
 &#125;
 </script>
 </head>
 <body>
<a href="#" onclick="ajax()">click here1</a>
<div id="mydiv"></div>
</body>
</html>
</textarea><br/>a2.jsp代码<br/><textarea name="code" class="html" rows="15" cols="100">
<script type="text/javascript">
showHi = function() &#123;
 alert('Hi');
&#125;
sayHello = function() &#123;
 alert('Hello');
&#125;
 </script>
<a href="#" onclick="showHi();">showHi</a>
<script type="text/javascript">
 sayHello();
</script>
</textarea><br/>Tags - <a href="https://blog.liuts.com/tags/prototype/" rel="tag">prototype</a>
]]>
</description>
</item>
</channel>
</rss>