ajax只支持utf-8格式,不能支持gb2312编码格式,所以经常遇到gb2312的编码的程序使用ajax就出现乱码,刚找到一种解决方案是:
服务器端传送的数据仍是gb2312编码,客户端用js将汉字转变成utf8编码显示在页面
方法一json
一,服务器端json数据用php的iconv函数转换:iconv('gb2312', 'utf8', "被转换字符串,输出到浏览器");
客户端获取utf8数据,再转成gb2312:
- function gb2utf8(data){//gb编码是,ie通过二进制码utf8->gbk转为中文
- var glbencode = [];
- gb2utf8_data = data;
- execscript("gb2utf8_data = midb(gb2utf8_data, 1)", "vbscript");
- var t=escape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g|>,"%$2%$1").replace(/%([a-z].)%(.{2})/g|>,"@$1$2");
- t=t.split("@");
- var i=0,j=t.length,k;
- while(++i<j)>
- k=t.substring(0,4);
- if(!glbencode[k]) {
- gb2utf8_char = eval("0x"+k);
- execscript("gb2utf8_char = chr(gb2utf8_char)", "vbscript");
- glbencode[k]=escape(gb2utf8_char).substring(1,6);
- }
- t=glbencode[k]+t.substring(4);
- }
- gb2utf8_data = gb2utf8_char = null;
- 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
- <?php
- header("content-type: text/html; charset=gb2312");
- include './search.htm';
- ?>
- search.htm
- <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=gb2312" />
- <title>高级搜索</title>
- </head>
- <body>
- <h3>高级搜索</h3>
- <form method="post" action="">
- 学校类型:
- <select name="schooltype">
- <option value="">全部</option>
- <option value="1">小学</option>
- <option value="2">初中</option>
- </select>
- 学校名称:
- <select name="sid" id="sid">
- <option value="">请选择学校</option>
- </select>
- </form>
- <script type="text/">
- function ajax(settings) {
- var xhr = window.activexobject ? new activexobject("microsoft.xmlhttp") : new xmlhttprequest(), successed = false;
- xhr.open(settings.type, settings.url);
- if(settings.type == 'post')
- xhr.setrequestheader('content-type', 'application/x-www-form-urlencoded');
- xhr.send((!settings.cache ? 'time=' + new date().gettime() + '&' : '') + settings.data);
- settings.loader();
- settimeout(function() {
- if(!successed) {
- alert('resquest timeout!');
- xhr.abort();
- }
- }, settings.timeout);
- xhr.onreadystatechange = function() {
- if (xhr.readystate == 4 && xhr.status == 200) {
- settings.callback(xhr.responsetext.replace(/(^s*)|(s*$)/g, ""));
- }
- successed = true;
- }
- }
- function a(t) {
- ajax({
- type: 'post',
- url: 'ajax.php',
- data: 'schooltype=' + t,
- timeout: 8000,
- cache: true,
- loader: function() {},
- callback: function(d) {
- var arr = eval(d);
- if(typeof(arr) == 'object') {
- var obj, option;
- document.getelementbyid('sid').innerhtml = '';
- for(var i = 0; obj = arr; i ++) {
- option = document.createelement('option');
- option.value = obj[0];
- option.innerhtml = txt2utf8(obj[1], '&#');
- document.getelementbyid('sid').appendchild(option);
- }
- }
- }
- })
- }
- function txt2utf8(string, prefix){
- for(var i=0,utf8=[];i<string.length;utf8.push((prefix||'u')+string.charcodeat(i++)));
- return utf8.join('');
- }
- a(0);
- </script>
- </body>
- </html>
- ajax.php
- <?php
- header("content-type: text/html; charset=gb2312");
- $schooltype = !emptyempty($_post['schooltype']) ? $_post['schooltype'] : 0;
- switch($schooltype) {
- case 0:
- echo "[['40', '太平溪镇花栗包完全小学'],['41', '太平溪镇长岭黑龙江希望小学'],['42', '乐天溪镇初级中学'],['43', '乐天溪镇莲沱初级中学']]";
- break;
- case 1:
- echo "[['40', '太平溪镇花栗包完全小学'],['41', '太平溪镇长岭黑龙江希望小学']]";
- break;
- case 2:
- echo "[['42', '乐天溪镇初级中学'],['43', '乐天溪镇莲沱初级中学']]";
- break;
- default:
- break;
- }
- ?>
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
波比源码 » php ajax交互汉字乱码的问题解决方法
波比源码 » php ajax交互汉字乱码的问题解决方法
levaquin 250mg us order levofloxacin for sale
oral dutasteride ondansetron online order ondansetron pill
order spironolactone 25mg online cheap generic aldactone 100mg fluconazole 200mg us
ampicillin 250mg usa bactrim 960mg oral buy erythromycin 500mg online cheap
buy sildenafil without prescription fildena drug methocarbamol 500mg sale
sildenafil 50mg canada sildenafil 150mg for sale purchase estradiol
buy lamotrigine online cheap tretinoin over the counter tretinoin for sale online
tadalafil 10mg tablet tadalis 20mg price voltaren 50mg pill
accutane 40mg tablet purchase zithromax generic azithromycin 500mg usa
indocin uk buy amoxicillin 500mg pills cheap amoxicillin
cialis 10mg over the counter no prescription cialis viagra australia
buy arimidex for sale cheap pfizer viagra online viagra 50mg tablet
prednisone 40mg pills generic cialis com cheap viagra generic
generic accutane order accutane 40mg without prescription ivermectin 3mg tablets price
buy clonidine 0.1 mg sale meclizine 25mg usa order tiotropium bromide 9mcg for sale
buy buspirone 10mg without prescription buspirone 10mg cheap oxybutynin without prescription
hytrin 1mg price terazosin canada cheap azulfidine
purchase fosamax for sale buy fosamax 35mg without prescription pepcid 40mg tablet
tacrolimus 1mg tablet order ropinirole 2mg sale ursodiol 150mg canada
oral imdur telmisartan 80mg over the counter telmisartan over the counter
zoloft sale escitalopram 20mg cheap order sildenafil 50mg online
generic imuran 100 mcg order imuran pills sildenafil 100mg over the counter
buy dapsone 100 mg generic buy fexofenadine sale buy aceon
buy fluvoxamine 50mg pills fluvoxamine 100mg pill glucotrol order online
accutane sale deltasone 40mg pills prednisone 5mg oral
purchase zithromax generic order gabapentin 800mg gabapentin 100mg drug
olanzapine ca order generic zyprexa 10mg buy valsartan 80mg sale
order amlodipine 5mg online cheap norvasc us cialis for sale online
buy clozaril 50mg generic combivent 100 mcg price decadron 0,0,5 mg cost
order generic sildenafil 50mg sildenafil over counter buy lisinopril sale
buy omeprazole 10mg pills essay writing assistance real casino slots online
lopressor pill buy vardenafil 10mg without prescription levitra canada
buy an essay paper free casino games free roulette games
custom dissertation research paper assistance sildenafil mail order usa
buy clomid 100mg online cheap albuterol cheap real casino games
triamcinolone sale triamcinolone 10mg oral clarinex 5mg us
canadian cialis and healthcare order clopidogrel 75mg generic buy clopidogrel 150mg for sale
buy methotrexate 5mg generic order metoclopramide 10mg without prescription purchase reglan generic
buy losartan 50mg buy losartan 25mg sale topamax for sale online
ozobax generic zanaflex tablet ketorolac cost
gloperba cheap play money poker online blackjack poker online
roulette online online blackjack best best poker online real money
buy flomax online cheap aldactone price order generic spironolactone 25mg
diflucan 100mg cost order viagra 100mg sale order sildenafil 50mg online
cost tadalafil 5mg order cialis 40mg sale sildenafil 100mg over the counter
sildenafil 50mg us buy generic viagra cialis in usa
buy ceftin 250mg for sale ceftin 250mg cost robaxin 500mg oral
my friend essay writing write college essays for money ivermectin for humans walmart
prednisone buy online brand accutane 20mg buy amoxicillin sale
online casino no deposit bonus free spins modafinil 200mg tablet
purchase furosemide pills hydroxychloroquine 400mg brand cost plaquenil 200mg
generic deltasone 20mg order minipress sale order mebendazole 100mg online
order fildena sale order tamoxifen 20mg without prescription cheap budesonide
tretinoin gel generic order tretinoin gel for sale avanafil 100mg over the counter
cost tadalafil tadacip 20mg generic purchase indocin without prescription
buy terbinafine 250mg for sale buy terbinafine online cheap order amoxicillin 250mg generic
order proventil 100 mcg generic buy ciprofloxacin online cheap cipro oral
actos 15mg cost cheap sildenafil for sale sildenafil 20mg
cost cialis 20mg Get cialis online free slot games for fun
best essay writing websites order azulfidine online cheap buy azulfidine for sale
doxycycline 200mg price doxycycline 100mg cost buy clindamycin generic
lanoxin online digoxin tablet buy molnupiravir online
carvedilol 25mg for sale ditropan 5mg cost buy amitriptyline generic
priligy 60mg cost purchase motilium without prescription brand domperidone 10mg
order cialis 5mg pill order viagra 100mg sale buy viagra 100mg sale
buy minocin 100mg sale buy hytrin pills terazosin 1mg price
order generic modafinil purchase stromectol order generic phenergan
clomid without prescription clomid cost order prednisolone 10mg generic
prednisone generic order deltasone 10mg online amoxil 500mg cost
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…
isotretinoin 10mg drug prednisone 5mg canada acillin sale
brand sildenafil fildena 50mg cost purchase propecia without prescription
stromectol how much it cost cost prednisone order prednisone 10mg online cheap
zofran us buy bactrim bactrim online
order accutane 40mg online cheap order amoxil 1000mg buy zithromax 500mg online cheap
prednisolone 5mg for sale order prednisolone 5mg without prescription order lasix 100mg for sale
purchase provigil metoprolol 100mg oral order metoprolol pill
generic avodart avodart 0.5mg cost where to buy xenical without a prescription
doxycycline price order zovirax 400mg pill zovirax pill