关于asp:使用AJAX实现UTF8编码表单提交到GBK编码脚本无乱码的解决方法

25次阅读

共计 8020 个字符,预计需要花费 21 分钟才能阅读完成。

这篇文章次要介绍了用 AJAX 实现,多个不同编码的我的项目(GBK,UTF8)相互通过 POST/GET 汇总表单数据。最终实现 应用同一个页面接管 不同编码的 表单(GBK,UTF8),特地适宜 JS 采集我的项目, 须要的敌人能够参考下

1、表单我的项目 1 GBK 编码格局

用户采集单是 GBK 编码,通过 GET/POST 提交上来的数据也是 GBK 格局,无需做任何解决

测试源码

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="GB2312">
</head>
<body>
<li><a href="gbkForm.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >GBK 同编码表单不乱码 </a>
<li><a href="utf8Form.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >UTF8 表单批改前乱码 </a>
<li><a href="utf8FormRepair.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >UTF8 表单批改为 AJAX 计划后不乱码 </a>
<li><a href="otherForm.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 其余 GBK 同编码表单不乱码 </a>
<li><a href="http://www.miaoqiyuan.cn/p/utf8-submit-gbk-ajax-autocharset" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 测试程序阐明 </a></a>
<hr />
<h2>GET:</h2>
<form action="gbkSubmit.asp" method="GET" >
QQ:<input name="qq" value="QQ" /><br />
姓名:<input name="name" value="姓名"/><br />
地址:<input name="address" value="地址地址地址"/><br />
<button type="submit">GBK 表单提交到 GBK 脚本后不会乱码 </button>
</form>
<h2>POST:</h2>
<form action="gbkSubmit.asp" method="POST" >
QQ:<input name="qq" value="QQ" /><br />
姓名:<input name="name" value="姓名"/><br />
地址:<input name="address" value="地址地址地址"/><br />
<button type="submit">GBK 表单提交到 GBK 脚本后不会乱码 </button>
</form>
</body>
</html>

2、表单我的项目 2 UTF8 编码格局(能够在不同的域名下)

用户采集单是 UTF8 编码,通过 GET/POST 提交上来的数据也是 UTF8 格局,用表单我的项目 1 中的程序接管到的是乱码

测试代码(批改前,提交乱码):

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF8">
</head>
<body>
<li><a href="gbkForm.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >GBK 同编码表单不乱码 </a>
<li><a href="utf8Form.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >UTF8 表单批改前乱码 </a>
<li><a href="utf8FormRepair.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >UTF8 表单批改为 AJAX 计划后不乱码 </a>
<li><a href="otherForm.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 其余 GBK 同编码表单不乱码 </a>
<li><a href="http://www.miaoqiyuan.cn/p/utf8-submit-gbk-ajax-autocharset" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 测试程序阐明 </a></a>
<hr />
<h2>GET:</h2>
<form action="gbkSubmit.asp" method="GET" >
QQ:<input name="qq" value="QQ" /><br />
姓名:<input name="name" value="姓名"/><br />
地址:<input name="address" value="地址地址地址"/><br />
<button type="submit">UTF8 表单提交到 GBK 脚本后会乱码 </button>
</form>
<h2>POST:</h2>
<form action="gbkSubmit.asp" method="POST" >
QQ:<input name="qq" value="QQ" /><br />
姓名:<input name="name" value="姓名"/><br />
地址:<input name="address" value="地址地址地址"/><br />
<button type="submit">UTF8 表单提交到 GBK 脚本后会乱码 </button>
</form>
</body>
</html>

不须要扭转页面编码,只须要批改表单 Action 地址,减少 name=”ConvertCallBack”value=”ProjectA/FormGBK”、name=”ConvertItems”value=”name,qq,bb”的两个 input 即可,不必对页面进行其余调整
编码设置,转换器主动操作。

<form action="convert/utf8.asp"...
<input name="ConvertCallBack" value="ProjectA/FormGBK">
<input name="ConvertItems" value="name,qq,address"/>
<!-- 依据表单理论状况,批改表单我的项目 -->

测试代码(批改后)

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF8">
</head>
<body>
<li><a href="gbkForm.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >GBK 同编码表单不乱码 </a>
<li><a href="utf8Form.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >UTF8 表单批改前乱码 </a>
<li><a href="utf8FormRepair.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >UTF8 表单批改为 AJAX 计划后不乱码 </a>
<li><a href="otherForm.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 其余 GBK 同编码表单不乱码 </a>
<li><a href="http://www.miaoqiyuan.cn/p/utf8-submit-gbk-ajax-autocharset" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 测试程序阐明 </a></a>
<hr />
<h2>GET:</h2>
<form action="convert/utf8.asp" method="GET" >
QQ:<input name="qq" value="QQ" /><br />
姓名:<input name="name" value="姓名"/><br />
地址:<input name="address" value="地址地址地址"/><br />
<input name="ConvertCallBack" value="../gbkSubmit.asp" />
<input name="ConvertItems" value="name,qq,address" />
<button type="submit">AJAX 转码计划 </button>
</form>
<h2>POST:</h2>
<form action="convert/utf8.asp" method="POST" >
QQ:<input name="qq" value="QQ" /><br />
姓名:<input name="name" value="姓名"/><br />
地址:<input name="address" value="地址地址地址"/><br />
<input name="ConvertCallBack" value="../gbkSubmit.asp" />
<input name="ConvertItems" value="name,qq,address" />
<button type="submit">AJAX 转码计划 </button>
</form>
</body>
</html>

3、表单我的项目 3 GBK 编码格局(能够在不同的域名下)

没有任何影响,只有依照表单 1 的规范编写 参数即可
测试代码:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="GB2312">
</head>
<body>
<li><a href="gbkForm.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >GBK 同编码表单不乱码 </a>
<li><a href="utf8Form.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >UTF8 表单批改前乱码 </a>
<li><a href="utf8FormRepair.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >UTF8 表单批改为 AJAX 计划后不乱码 </a>
<li><a href="otherForm.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 其余 GBK 同编码表单不乱码 </a>
<li><a href="http://www.miaoqiyuan.cn/p/utf8-submit-gbk-ajax-autocharset" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 测试程序阐明 </a></a>
<hr />
<h2>GET:</h2>
<form action="gbkSubmit.asp" method="GET" >
QQ:<input name="qq" value="QQ" /><br />
姓名:<input name="name" value="姓名"/><br />
地址:<input name="address" value="地址地址地址"/><br />
Other1:<input name="other1" value="Other1"/><br />
Other2:<input name="other2" value="Other2"/><br />
Other3:<input name="other3" value="Other3"/><br />
Other4:<input name="other4" value="Other4"/><br />
Other5:<input name="other5" value="Other5"/><br />
Other6:<input name="other6" value="Other6"/><br />
Other7:<input name="other7" value="Other7"/><br />
Other8:<input name="other8" value="Other8"/><br />
Other9:<input name="other9" value="Other9"/><br />
<button type="submit">GBK 表单提交到 GBK 脚本后不会乱码 </button>
</form>
<h2>POST:</h2>
<form action="gbkSubmit.asp" method="POST" >
QQ:<input name="qq" value="QQ" /><br />
姓名:<input name="name" value="姓名"/><br />
地址:<input name="address" value="地址地址地址"/><br />
Other1:<input name="other1" value="Other1"/><br />
Other2:<input name="other2" value="Other2"/><br />
Other3:<input name="other3" value="Other3"/><br />
Other4:<input name="other4" value="Other4"/><br />
Other5:<input name="other5" value="Other5"/><br />
Other6:<input name="other6" value="Other6"/><br />
Other7:<input name="other7" value="Other7"/><br />
Other8:<input name="other8" value="Other8"/><br />
Other9:<input name="other9" value="Other9"/><br />
<button type="submit">GBK 表单提交到 GBK 脚本后不会乱码 </button>
</form>
</body>
</html>

全副测试包下载地址 anycharset_jb51.rar

utf8.asp

<% @codepage=936 %>
<%
  CallBack = Server.Htmlencode(Request("ConvertCallBack"))
  Items = Split(Request("ConvertItems"),",")
  if len(request.form) > 0 then
    method = "POST"
  else
    method = "GET"
  end if
%><!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="GB2312">
<script src="http://www.miaoqiyuan.cn/wp-includes/js/jquery/jquery.js"></script>
</head>
<body>
<form action="<%=CallBack%>" method="<%=method%>" id="AutoPOST">
<%
  for each Item in Items
    if trim(Item) <> "" then
      Item = Server.Htmlencode(Item)
%><% = Item %> : <textarea name="<% = Item %>" id="<% = Item %>"></textarea><br />
<script>
jQuery.post("utf8_convert.asp?MiaoQiYuanAutoConvertItem=<%=Item%>&<%=Request.QueryString%>","<%=Request.Form%>",function(res){jQuery("#<% = Item %>").val(res);
});
</script>
<%
    end if
  next
%>
<button type="submit">Waiting...</button>
<script type="text/javascript">
setTimeout(function(){jQuery("#AutoPOST").submit();},200);
</script>
</body>
</html>

utf8_convert.asp

<% @codepage=65001 %>
<%
  function NotXSS(html)
    NotXSS = html
    NotXSS = replace(NotXSS, "<", "&lt;")
    NotXSS = replace(NotXSS, ">", "&gt;")
    NotXSS = replace(NotXSS, """","&quote;")
  end function
    
  ' 避免参数抵触,应用了 MiaoQiYuanAutoConvertItem
  Response.Write NotXSS(Request(Request.QueryString("MiaoQiYuanAutoConvertItem")))
%>

到此这篇对于应用 AJAX 实现 UTF8 编码表单提交到 GBK 编码脚本无乱码的解决办法的文章就介绍到这了, 更多相干 UTF8 编码表单提交到 GBK 编码内容请搜寻 segmentfault 以前的文章或持续浏览上面的相干文章心愿大家当前多多反对 segmentfault!

正文完
 0