欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-09 17:13:53
1、表单项目1 gbk编码格式用户采集单是gbk编码,通过get/post提交上来的数据也是gbk格式,无需做任何处理测试源码

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>

全部测试包下载地址

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编码内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: UTF8 编码 GBK