ajax交互汉字乱码的主题素材化解措施,消除编码

缓和编码为gb2312页面ajax交互汉字乱码难题
ajax只支持utf-8格式,不能够支撑gb2312编码格式,所以常常碰着gb2312的编码的程序行使ajax就出现乱码,刚找到一种缓慢解决方案是:

服务器端传送的数目仍是gb2312编码,客商端用js将汉字转变成utf8编码展现在页面

劳务器端传送的数码仍是gb2312编码,顾客端用js将汉字转换成utf8编码展现在页面

方法一json
一,服务器端json数据用php教程的iconv函数调换:iconv('gb2312', 'utf8', "被更动字符串,输出到浏览器"); 
      顾客端获取utf8数据,再转成gb2312:

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" ";
<html xmlns=";
<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>

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("%")); 
}

ajax.php

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

<?php
header("content-type: text/html; charset=gb2312");
$schooltype = !empty($_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;
}
?>

三,在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" ";
<html xmlns=";
<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 = !empty($_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;
}
?>

本文由365bet体育在线官网发布于365bet,转载请注明出处:ajax交互汉字乱码的主题素材化解措施,消除编码

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。