您现在的位置: 万盛学电脑网 >> 程序编程 >> 脚本专题 >> javascript >> 正文

js ajax中传递中文参数处理

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   ajax中传递中文参数处理代码:

 代码如下  

js中:

send_request('http://www.45it.com /mini_do.php?username=' + encodeURI(username) + '&phone=' + encodeURI(phone)+ '&content=' + encodeURI(content));

mini_do.php中:

$username = urldecode($_REQUEST[username]);
$phone = urldecode($_REQUEST[phone]);
$content = urldecode($_REQUEST[content]);

  最后有朋友说可以使用escape函数

  var htmer ="getcode="+escape(getcode)+"&Content="+escape(Content);

  这个本人不建义哦因为它出错更高有些汉字字符不能识别,还有一个函数encodeURIComponent这个比较全面了

  encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)

  总结

  从上面来看

  escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z

  encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z

  encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z

  编码范围来看我们选择encodeURI是最佳的选择了。