使用AJAX实现UTF8编码表单提交到GBK编码脚本无乱码
网络编程 2021-07-05 10:58www.168986.cn编程入门
这篇文章主要介绍了用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://.miaoqiyuan./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://.miaoqiyuan./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://.miaoqiyuan./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://.miaoqiyuan./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>
全部测试包下载地址
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://.miaoqiyuan./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, "<", "<") NotXSS = replace(NotXSS, ">", ">") NotXSS = replace(NotXSS, """", ""e;") end function '防止参数冲突,使用了 MiaoQiYuanAutoConvertItem Response.Write NotXSS(Request(Request.QueryString("MiaoQiYuanAutoConvertItem"))) %>
到此这篇关于使用AJAX实现UTF8编码表单提交到GBK编码脚本无乱码的解决方法的文章就介绍到这了,更多相关UTF8编码表单提交到GBK编码内容请搜索狼蚁SEO以前的文章或继续浏览狼蚁网站SEO优化的相关文章希望大家以后多多支持狼蚁SEO!
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程