UTF-8字符集在AJAX下中文乱码的解决办法

发布于 2022-09-12 21:29:51 字数 4665 浏览 9 评论 0

转自http://bbs.lampbrother.net/read-htm-tid-2610.html

本以为全部UTF-8 就不用关心乱码问题了呢 , 结果乱码还是来了.. 查了点英文资料 , 搞定.

方法很简单 , 直接对提交的数据进行编码即可..

但是 UTF-8 的 和ASCII的不怎么一样.. 下面是修正办法

var str = document.frm.username.value
修改成
var str = encode(document.frm.username.value)

然后在 这个标记前面 把下面代码复制进去

  1. var unreserved = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.~";
  2. var reserved = "!*'();:@&=+$,/?%#[]";
  3. var allowed = unreserved + reserved;
  4. var hexchars = "0123456789ABCDEFabcdef";
  5. // --------------------------------- Encoding -------------------------------
  6. // This function returns a percent sign followed by two hexadecimal digits.
  7. // Input is a decimal value not greater than 255.
  8. function gethex(decimal) {
  9. return "%" + hexchars.charAt(decimal >> 4) + hexchars.charAt(decimal & 0xF);
  10. }
  11. function encode(str) {
  12. // Clear output field:
  13. // Some variables:
  14. var decoded = str;
  15. var encoded = "";
  16. for (var i = 0; i < decoded.length; i++ ) {
  17. var ch = decoded.charAt(i);
  18. // Check if character is an unreserved character:
  19. if (unreserved.indexOf(ch) != -1) {
  20. encoded = encoded + ch;
  21. } else {
  22. // The position in the Unicode table tells us how many bytes are needed.
  23. // Note that if we talk about first, second, etc. in the following, we are
  24. // counting from left to right:
  25. //
  26. // Position in | Bytes needed | Binary representation
  27. // Unicode table | for UTF-8 | of UTF-8
  28. // ----------------------------------------------------------
  29. // 0 - 127 | 1 byte | 0XXX.XXXX
  30. // 128 - 2047 | 2 bytes | 110X.XXXX 10XX.XXXX
  31. // 2048 - 65535 | 3 bytes | 1110.XXXX 10XX.XXXX 10XX.XXXX
  32. // 65536 - 2097151 | 4 bytes | 1111.0XXX 10XX.XXXX 10XX.XXXX 10XX.XXXX
  33. var charcode = decoded.charCodeAt(i);
  34. // Position 0 - 127 is equal to percent-encoding with an ASCII character encoding:
  35. if (charcode < 128) {
  36. encoded = encoded + gethex(charcode);
  37. }
  38. // Position 128 - 2047: two bytes for UTF-8 character encoding.
  39. if (charcode > 127 && charcode < 2048) {
  40. // First UTF byte: Mask the first five bits of charcode with binary 110X.XXXX:
  41. encoded = encoded + gethex((charcode >> 6) | 0xC0);
  42. // Second UTF byte: Get last six bits of charcode and mask them with binary 10XX.XXXX:
  43. encoded = encoded + gethex((charcode & 0x3F) | 0x80);
  44. }
  45. // Position 2048 - 65535: three bytes for UTF-8 character encoding.
  46. if (charcode > 2047 && charcode < 65536) {
  47. // First UTF byte: Mask the first four bits of charcode with binary 1110.XXXX:
  48. encoded = encoded + gethex((charcode >> 12) | 0xE0);
  49. // Second UTF byte: Get the next six bits of charcode and mask them binary 10XX.XXXX:
  50. encoded = encoded + gethex(((charcode >> 6) & 0x3F) | 0x80);
  51. // Third UTF byte: Get the last six bits of charcode and mask them binary 10XX.XXXX:
  52. encoded = encoded + gethex((charcode & 0x3F) | 0x80);
  53. }
  54. // Position 65536 - : four bytes for UTF-8 character encoding.
  55. if (charcode > 65535) {
  56. // First UTF byte: Mask the first three bits of charcode with binary 1111.0XXX:
  57. encoded = encoded + gethex((charcode >> 18) | 0xF0);
  58. // Second UTF byte: Get the next six bits of charcode and mask them binary 10XX.XXXX:
  59. encoded = encoded + gethex(((charcode >> 12) & 0x3F) | 0x80);
  60. // Third UTF byte: Get the last six bits of charcode and mask them binary 10XX.XXXX:
  61. encoded = encoded + gethex(((charcode >> 6) & 0x3F) | 0x80);
  62. // Fourth UTF byte: Get the last six bits of charcode and mask them binary 10XX.XXXX:
  63. encoded = encoded + gethex((charcode & 0x3F) | 0x80);
  64. }
  65. }
  66. } // end of for ...
  67. // Write result:
  68. return encoded;
  69. }

复制代码ok,大功告成!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文