php ajax交互汉字乱码的问题解决方法

ajax只支持utf-8格式,不能支持gb2312编码格式,所以经常遇到gb2312的编码的程序使用ajax就出现乱码,刚找到一种解决方案是:

服务器端传送的数据仍是gb2312编码,客户端用js将汉字转变成utf8编码显示在页面

方法一json

一,服务器端json数据用php的iconv函数转换:iconv('gb2312', 'utf8', "被转换字符串,输出到浏览器");

客户端获取utf8数据,再转成gb2312:

  1. function gb2utf8(data){//gb编码是,ie通过二进制码utf8->gbk转为中文   
  2.      var glbencode = [];   
  3.      gb2utf8_data = data;   
  4.      execscript("gb2utf8_data = midb(gb2utf8_data, 1)""vbscript");   
  5.      var t=escape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g|>,"%$2%$1").replace(/%([a-z].)%(.{2})/g|>,"@$1$2");   
  6.      t=t.split("@");   
  7.      var i=0,j=t.length,k;   
  8.      while(++i<j)>   
  9.          k=t.substring(0,4);   
  10.          if(!glbencode[k]) {   
  11.              gb2utf8_char = eval("0x"+k);   
  12.              execscript("gb2utf8_char = chr(gb2utf8_char)""vbscript");   
  13.              glbencode[k]=escape(gb2utf8_char).substring(1,6);   
  14.          }   
  15.          t=glbencode[k]+t.substring(4);   
  16.      }   
  17.      gb2utf8_data = gb2utf8_char = null;   
  18.      return unescape(t.join("%"));   

二,header("content-type", "application/x-www-form-urlencoded; charset=gbk"); //输出头标,设置为gbk编码

三,在ajax请求数据前调用上面的方法指定请求使用的字符集:xmlhttp.setrequestheader( "content-type", "application/x-www-form-urlencoded;charset=gbk");

方案二:search.php

  1. <?php 
  2. header("content-type: text/html; charset=gb2312"); 
  3. include './search.htm'
  4. ?> 
  5. search.htm 
  6. <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"
  7. <html xmlns="http://www.w3.org/1999/xhtml"
  8. <head> 
  9. <meta http-equiv="content-type" content="text/html; charset=gb2312" /> 
  10. <title>高级搜索</title> 
  11. </head> 
  12. <body> 
  13. <h3>高级搜索</h3> 
  14. <form method="post" action=""
  15.   学校类型: 
  16.   <select name="schooltype"
  17.     <option value="">全部</option> 
  18.     <option value="1">小学</option> 
  19.     <option value="2">初中</option> 
  20.   </select> 
  21.   学校名称: 
  22.   <select name="sid" id="sid"
  23.     <option value="">请选择学校</option> 
  24.   </select> 
  25. </form> 
  26. <script type="text/"
  27. function ajax(settings) { 
  28.     var xhr = window.activexobject ? new activexobject("microsoft.xmlhttp") : new xmlhttprequest(), successed = false; 
  29.     xhr.open(settings.type, settings.url); 
  30.     if(settings.type == 'post'
  31.      xhr.setrequestheader('content-type''application/x-www-form-urlencoded'); 
  32.     xhr.send((!settings.cache ? 'time=' + new date().gettime() + '&' : '') + settings.data); 
  33.     settings.loader(); 
  34.     settimeout(function() { 
  35.         if(!successed) { 
  36.             alert('resquest timeout!'); 
  37.             xhr.abort(); 
  38.         } 
  39.     }, settings.timeout); 
  40.     xhr.onreadystatechange = function() { 
  41.         if (xhr.readystate == 4 && xhr.status == 200) { 
  42.             settings.callback(xhr.responsetext.replace(/(^s*)|(s*$)/g, "")); 
  43.         } 
  44.         successed = true; 
  45.     } 
  46. function a(t) { 
  47. ajax({ 
  48.   type: 'post'
  49.   url: 'ajax.php'
  50.   data: 'schooltype=' + t, 
  51.   timeout: 8000, 
  52.   cache: true, 
  53.   loader: function() {}, 
  54.   callback: function(d) { 
  55.    var arr = eval(d); 
  56.    if(typeof(arr) == 'object') { 
  57.     var obj, option; 
  58.     document.getelementbyid('sid').innerhtml = ''
  59.     for(var i = 0; obj = arr; i ++) { 
  60.      option = document.createelement('option'); 
  61.      option.value = obj[0]; 
  62.      option.innerhtml = txt2utf8(obj[1], '&#'); 
  63.      document.getelementbyid('sid').appendchild(option); 
  64.     } 
  65.    } 
  66.   } 
  67. }) 
  68. function txt2utf8(string, prefix){ 
  69.     for(var i=0,utf8=[];i<string.length;utf8.push((prefix||'u')+string.charcodeat(i++))); 
  70.     return utf8.join(''); 
  71. a(0); 
  72. </script> 
  73. </body> 
  74. </html> 
  75. ajax.php 
  76. <?php 
  77. header("content-type: text/html; charset=gb2312"); 
  78. $schooltype = !emptyempty($_post['schooltype']) ? $_post['schooltype'] : 0; 
  79. switch($schooltype) { 
  80.     case 0: 
  81.         echo "[['40', '太平溪镇花栗包完全小学'],['41', '太平溪镇长岭黑龙江希望小学'],['42', '乐天溪镇初级中学'],['43', '乐天溪镇莲沱初级中学']]"
  82.         break
  83.     case 1: 
  84.         echo "[['40', '太平溪镇花栗包完全小学'],['41', '太平溪镇长岭黑龙江希望小学']]"
  85.         break
  86.     case 2: 
  87.         echo "[['42', '乐天溪镇初级中学'],['43', '乐天溪镇莲沱初级中学']]"
  88.         break
  89.     default
  90.         break
  91. ?> 
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » php ajax交互汉字乱码的问题解决方法

79 评论

  1. ampicillin 250mg usa bactrim 960mg oral buy erythromycin 500mg online cheap

  2. buy sildenafil without prescription fildena drug methocarbamol 500mg sale

  3. buy clonidine 0.1 mg sale meclizine 25mg usa order tiotropium bromide 9mcg for sale

  4. buy buspirone 10mg without prescription buspirone 10mg cheap oxybutynin without prescription

  5. generic imuran 100 mcg order imuran pills sildenafil 100mg over the counter

  6. order amlodipine 5mg online cheap norvasc us cialis for sale online

  7. buy clomid 100mg online cheap albuterol cheap real casino games

  8. buy flomax online cheap aldactone price order generic spironolactone 25mg

  9. online casino no deposit bonus free spins modafinil 200mg tablet

  10. lanoxin online digoxin tablet buy molnupiravir online

  11. clomid without prescription clomid cost order prednisolone 10mg generic

  12. Great post thank you. Hello Administ . Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…

  13. brand sildenafil fildena 50mg cost purchase propecia without prescription

  14. stromectol how much it cost cost prednisone order prednisone 10mg online cheap

  15. order accutane 40mg online cheap order amoxil 1000mg buy zithromax 500mg online cheap

  16. generic avodart avodart 0.5mg cost where to buy xenical without a prescription

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡