现在的位置: 首页 > 综合 > 正文

URL 编码乱码问题

2018年08月19日 ⁄ 综合 ⁄ 共 624字 ⁄ 字号 评论关闭

javascript中可用的编码解码函数,有如下的组合:

  • escape(string);
    unescape(string);
  • encodeURI(string);
    decodeURI(string);
  • encodeURIComponent(string);
    decodeURIComponent(string);

他们之间的区别为:

escape/unescape:
以16进制编码字符串,对空格、符号等字符用%xx编码表示,对中文等字符用%uxxxx编码表示。自javascript1.5之后,此方法已经不被推荐使用。

encodeURI/decodeURI:
以UTF-8编码编码字符串,对这些字符:“; , / ? : @ & = + $”不做编码。

encodeURIComponent/decodeURIComponent:(推荐使用)
以UTF-8编码编码所有字符串。

  		 	var tit = $("TITLE").html(); //获取页面title 内容
  		 	var titles = encodeURIComponent(tit);//编码
			$.post("abcde.action",{title:titles});//post 方法传参 JQUERY

			String title = new String(title.get(i).getBytes("GBK"),"UTF-8");  //解码
			title=java.net.URLDecoder.decode(title,"UTF-8");/*需要处理异常*/  //解码


抱歉!评论已关闭.