AJAX提交数据时 中文处理 以及js url 中文处理

最近,了解到一些朋友又深陷其中,便把自己以前的解决方法拿出来晒晒,希望对误入该行业的朋友有些帮助。

  原理:html不能设置编码,但是xml可以,我们采用xml,把数据携带过去,不就可以了嘛。

  js核心代码:

//注:xmlDoc,xmlHtml对象创建请用更完善的方式,这里只是简单演示一下
function doSubmit(){
 var str=document.getElementById("input1").value;
 //假设str就是你要提交的数据
 alert(str);
 
 //"MSXML2.DOMDocument", "Microsoft.XMLDOM", "MSXML.DOMDocument", "MSXML3.DOMDocument"
 var xmlDoc=new ActiveXObject("MSXML2.DOMDocument");
 
 //初始化xml文档对象
 xmlDoc.loadXML("<html></html>");
 xmlDoc.documentElement.text=str;//作为内容去携带
 //如果采用属性携带数据更方便,那么可以用下面的方法
 //xmlDoc.documentElement.setAttribute("name","msg");
 //xmlDoc.documentElement.setAttribute("value",str);
  
 alert(xmlDoc.xml);//查看生成的xml内容
 
 //"MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp";
 var xmlHttp=new ActiveXObject("MSXML2.XMLHttp.5.0");
 var url="servlet/MyServlet?time="+(new Date()).getTime();
 xmlHttp.open("POST",url,false);
 xmlHttp.send(xmlDoc);//把xml对象发送出去
 alert(xmlHttp.responseText);
}

  servlet/action核心代码:

// 读取ajax发送来的xml数据
 SAXReader xmlReader = new SAXReader();
 Document document = null;
 try {
 document = xmlReader.read(request.getInputStream());
 } catch (Exception ex) {
 System.err.println("xml读取失败,可能没有xml数据.");
 ex.printStackTrace();
 }
 System.out.println("接收到xml数据:" + document.asXML());
  
 // 解析xml
 String str = document.getRootElement().getText();
 System.out.println("解析出来的数据:" + str);
  
 // 返回结果
 response.setContentType("text/html; charset=UTF-8");// GBK也行,指明返回的编码
 response.getWriter().print("服务器返回信息:成功啦!no(∩_∩)o...哈哈!");// 返回中文也没问题啦

---------------------------

在使用url进行参数传递时,经常会传递一些中文名的参数或URL地址,在后台处理时会发生转换错误。在有些传递页面使用GB2312,而在接收页面使用UTF8,这样接收到的参数就可能会与原来发生不一致。使用服务器端的urlEncode函数编码的URL,与使用客户端javascript的 encodeURI函数编码的URL,结果就不一样。

--------------------

其实url 中文处理可以使用两个编码的函数

escape 和 unescape 说明如下:

Encodes String objects so they can be read on all computers.

escape(
                      charString
                      ) 

The required charString argument is any String object or literal to be encoded.

The escape method returns a string value (in Unicode format) that contains the contents of charstring. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with % xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as "%20."

Characters with a value greater than 255 are stored using the %u xxxx format.

=============================================

Decodes String objects encoded with the escape method.

unescape(charString) 

The required charString argument is a String object or literal to be decoded.

The unescape method returns a string value that contains the contents of charstring. All characters encoded with the %xx hexadecimal form are replaced by their ASCII character set equivalents.

Characters encoded in %u xxxx format (Unicode characters) are replaced with the Unicode character with hexadecimal encoding xxxx.

Note

The unescape method should not be used to decode Uniform Resource Identifiers (URI). Use decodeURI and decodeURIComponent methods instead.

转载于:https://www.cnblogs.com/backuper/archive/2008/12/24/1361488.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值