php ajax交互汉字乱码的问题解决方法
来源:程序员人生 发布时间:2013-10-26 13:23:32 阅读次数:2565次
ajax只支持utf-8格式,不能支持gb2312编码格式,所以经常遇到gb2312的编码的程序使用ajax就出现乱码,刚找到一种解决方案是:
服务器端传送的数据仍是gb2312编码,客户端用js将汉字转变成utf8编码显示在页面
方法一json
一,服务器端json数据用php的iconv函数转换:iconv('gb2312', 'utf8', "被转换字符串,输出到浏览器");
客户端获取utf8数据,再转成gb2312:
- function gb2utf8(data){
- 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;
- }
- ?>
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠