最近估计是和utf-8干上了,今天又搞定一个ajax的编码问题

本文介绍了一种解决AJAX请求返回中文乱码的方法,通过使用VBScript进行编码转换,实现了正确显示中文内容。该方案已在IE浏览器上验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天开始试用ajax最为项目的辅助,本来开始很顺利,测试的内容也比较简单
先在一个jsp中加入ajax的引擎
//ajax 引擎
    <script language="javascript">
None.gif    var http_request 
= false;
None.gif    var currentPos 
= null;
ExpandedBlockStart.gifContractedBlock.gif    function send_request(url) 
dot.gif{//初始化、指定处理函数、发送请求的函数
InBlock.gif
        http_request = false;
InBlock.gif        
//开始初始化XMLHttpRequest对象
ExpandedSubBlockStart.gifContractedSubBlock.gif
        if(window.XMLHttpRequest) dot.gif//Mozilla 浏览器
InBlock.gif
            http_request = new XMLHttpRequest();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (http_request.overrideMimeType) dot.gif{//设置MiME类别
InBlock.gif
                http_request.overrideMimeType('text/xml');
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
else if (window.ActiveXObject) dot.gif// IE浏览器
ExpandedSubBlockStart.gifContractedSubBlock.gif
            try dot.gif{
InBlock.gif                http_request 
= new ActiveXObject("Msxml2.XMLHTTP");
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (e) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
try dot.gif{
InBlock.gif                    http_request 
= new ActiveXObject("Microsoft.XMLHTTP");
ExpandedSubBlockStart.gifContractedSubBlock.gif                }
 catch (e) dot.gif{}
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (!http_request) dot.gif// 异常,创建对象实例失败
InBlock.gif
            window.alert("不能创建XMLHttpRequest对象实例.");
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        http_request.onreadystatechange 
= processRequest;
InBlock.gif        
// 确定发送请求的方式和URL以及是否同步执行下段代码
InBlock.gif
        
InBlock.gif        http_request.open(
"POST", url, true);
InBlock.gif        http_request.setRequestHeader(
"Content-Type","application/x-www-form-urlencoded");
InBlock.gif            
InBlock.gif        http_request.send(
null);
ExpandedBlockEnd.gif    }

None.gif    
// 处理返回信息的函数
ExpandedBlockStart.gifContractedBlock.gif
    function processRequest() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (http_request.readyState == 4dot.gif// 判断对象状态
InBlock.gif
             
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (http_request.status == 200dot.gif// 信息已经成功返回,开始处理信息
InBlock.gif
                
InBlock.gif                InBlock.gif                
InBlock.gif                document.getElementById(currentPos).innerHTML 
= http_request.responseText;
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 else dot.gif//页面不正常
InBlock.gif
                alert("http_request.status exception code:"+http_request.status);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
</script>

然后在jsp中做个测试的select,和用于显示ajax的回传数据的区域
<table width="200" border="0" cellspacing="0" cellpadding="0">
    
<tr>
        
<td height="20"><select name="ajaxtest" size="1" onchange="showRoles('test1');">
        
<option selected="selected" value="test1" >test1option>
        
<option value="test2" >test2option>
        
<option value="test3" >test3option>
        
<option value="test4" >test4option>
        
select>td>
    
tr>
    
<tr style="display:none">
        
<td height="20" id="test1">&nbsp;td>
    
tr>
 
再写一个js用了作为事件的触发
<script language="javascript">
        
    
//显示部门下的岗位
    function showRoles(obj) {
        document.getElementById(obj).parentNode.style.display 
= "";
        document.getElementById(obj).innerHTML 
= "loading"
        currentPos 
= obj;
        
        send_request(
"http://localhost/oa-web/app/test/sample2_2.jsp?playPos="+obj);
    }
 再另外写一个资源jsp,用于读取资源回来


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
两个jsp的编码都是utf-8的,但是在测试时发现回传回来的responseText都是乱码
去google上找,原来http_request 这个对象在request和response都是以gb2312进行的,需要更改编码
但是可恶的是javascript没有提供更改http_request 编码的方法于是在网上找了一个vbscript的函数
<SCRIPT LANGUAGE="vbScript"> 
 Function URLEncoding(vstrIn) 
 strReturn 
= "" 
 For i 
= 1 To Len(vstrIn)
 ThisChr 
= Mid(vStrIn,i,1)
 If Abs(Asc(ThisChr)) 
< &HFF Then
 strReturn 
= strReturn & ThisChr 
 Else 
 innerCode 
= Asc(ThisChr)
 If innerCode 
< 0 Then 
 innerCode 
= innerCode + &H10000
 End If 
 Hight8 
= (innerCode And &HFF00)\ &HFF 
 Low8 
= innerCode And &HFF 
 strReturn 
= strReturn & "%" & Hex(Hight8) & "%" & Hex(Low8)
 End If
 Next 
 URLEncoding 
= strReturn
 End Function 
SCRIPT> 
然后对回传回来的http_request 进行一下编码更改
    // 处理返回信息的函数
    function processRequest() {
        
if (http_request.readyState == 4// 判断对象状态
             
            
if (http_request.status == 200// 信息已经成功返回,开始处理信息
                
                URLEncoding(http_request.responseBody);
                
                document.getElementById(currentPos).innerHTML 
= http_request.responseText;
            }
 else //页面不正常
                alert("http_request.status exception code:"+http_request.status);
            }

        }

    }
最终终于可以正常显示中文了
这两天和编码的纠缠有一个深刻的体会:做中国人难啊,要是计算机是中国人发明的多好:)
注:这种方法只在IE上试验过,别的浏览器还没有试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值