The client is encoded in UTF-8, which is also the standard encoding now recognized by everyone
In this case, when using AJAX to asynchronously obtain GB2312 encoded server-side information, you will inevitably encounter the problem of garbled Chinese characters
because The target data is GB2312, but XMLHttpRequest uses UTF-8 for data encapsulation by default, so garbled characters will be generated
I believe many people are using the lightweight JS tool set-prototype.js, and its AJAX function is also very good. Excellent
I have been using it, so I have been considering this problem based on prototype.js
But after many experiments, I still cannot convert the responseText it returns into the correct encoding format
Later I learned that the original data information is saved in the responseBody attribute of the XMLHttpRequest object
But the responseBody attribute returned by the AJAX function of prototype.js is undefined. It seems that I still have to do it myself
After nearly an hour After hammering, a short and concise AJAX framework was born, haha, but the functions are still very complete
Some of the writing methods borrowed from another lightweight AJAX framework-bingo.js implementation method
The calling method and Note:
myAjaxCall({
url : 'xxxxx .jsp' //Target page address
, params: URLEncoding('prm1=parameter 1&prm2=parameter 2') //Parameter string information
, method: 'POST' //Sending method POST or GET
,callBack: retValue //Callback function name
,isBody: true //Whether to return responseBody, responseText is returned by default
//,isXml: false //Whether to return data in XML format
//,errorReport: false //When sending an error, whether to pop up a prompt
//,attachs: {} //Additional parameters can be passed to the callback function
});
function retValue(res,att){
var strRet = bytes2BSTR(res);
alert(strRet);
}
Pay attention to the two functions:
, URLEncoding: for parameters Encoding
, bytes2BSTR: Decoding the returned data
These two functions directly draw on two popular encoding functions on the Internet, but they are both written in vbs
You need to Two functions are also attached to the above page:
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
Function bytes2BSTR(vIn)
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i 1,1))
strReturn = strReturn & Chr(CLng (ThisCharCode) * &H100 CInt(NextCharCode))
i = i 1
End If
Next
bytes2BSTR = strReturn
End Function
Attach me below Written lightweight Ajax framework - myAjax.js Source code:
/**
2 * myAjax
3 * by netwild
4 * netwild@163.com
5*/
6 var myAjaxConfig = {
7 "url":""
8 ,"params":""
9 ,"method":"GET"
,"callBack":function(){}
,"isXml":false
,"isBody":false
,"isCache":false
,"errorReport":true
,"statePoll":null
,"postData":null
,"attachs":{}
};
function myAjaxCall(requestJson){
var attach;
if(requestJson && typeof requestJson == "object"){
if(requestJson.url){myAjaxConfig.url = requestJson.url;}
if(requestJson.params){myAjaxConfig.params = requestJson.params;}
if(requestJson.method){myAjaxConfig.method = requestJson.method;}
if(requestJson.callBack){myAjaxConfig.callBack = requestJson.callBack;}
if(requestJson.isXml){myAjaxConfig.isXml = requestJson.isXml;}
if(requestJson.isBody){myAjaxConfig.isBody = requestJson.isBody;}
if(requestJson.isCache){myAjaxConfig.isCache = requestJson.isCache;}
if(requestJson.statePoll){myAjaxConfig.statePoll = requestJson.statePoll;}
if(requestJson.attachs){myAjaxConfig.attachs = requestJson.attachs;}
}
if(!myAjaxConfig.isCache){
var nocache = new Date().getTime();
if(myAjaxConfig.url.indexOf("?")>0){myAjaxConfig.url = "&nocache=" nocache;}
else{myAjaxConfig.url = "?nocache=" nocache;}
}
var newCall = new myAjaxCore();
newCall.init();
}
function myAjaxCore(){
var _self = this;
var _state,_status;
var _httpRequest,_attach;
////////////////////////////////////////////////////
this.init = function(){
if (window.XMLHttpRequest){
_httpRequest = new XMLHttpRequest();
if (_httpRequest.overrideMimeType) {
_httpRequest.overrideMimeType('text/xml');
}
}else if (window.ActiveXObject) {
var MSXML = ['MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
for(var n=0;ntry {
_httpRequest = new ActiveXObject(MSXML[n]);
break;
}catch(e){}
}
}
with(_httpRequest) {
onreadystatechange = _self.getResponse;
open(myAjaxConfig.method,myAjaxConfig.url,true);
if(myAjaxConfig.method == "POST" && (myAjaxConfig.params != "")){
setRequestHeader("Content-Length",myAjaxConfig.params.length);
setRequestHeader("Content-Type","application/x-www-form-urlencoded");
send(myAjaxConfig.params);
}else{
var textType = myAjaxConfig.isXml?"text/xml":"text/plain";
_httpRequest.setRequestHeader('Content-Type',textType);
if(browser.IE){
setRequestHeader("Accept-Encoding", "gzip, deflate");
}else if(browser.FF){
setRequestHeader("Connection","close");
}
send(null);
}
}
};
////////////////////////////////////////////////////
this.getResponse = function(){
_state = _httpRequest.readyState;
if(_httpRequest.readyState == 4 && _httpRequest.status){_status = _httpRequest.status;}
if(myAjaxConfig.statePoll){myAjaxConfig.statePoll(_httpRequest.readyState);}
if(_httpRequest.readyState==4 && _httpRequest.status>=400){
_self.abort();
_self.alertf("ERROR:HTTP response code " _httpRequest.status);
}
if(_httpRequest.readyState==4 && _httpRequest.status==200){
var response_content;
if(myAjaxConfig.isXML){
response_content = _httpRequest.responseXML;
}else if(myAjaxConfig.isBody){
response_content = _httpRequest.responseBody;
}else{
response_content = _httpRequest.responseText;
}
if(typeof myAjaxConfig.callBack == "function"){
myAjaxConfig.callBack(response_content,myAjaxConfig.attachs);
}else{
eval(myAjaxConfig.callBack "(response_content,myAjaxConfig.attachs)");
}
}
};
////////////////////////////////////////////////////
this.abort=function(){_httpRequest.abort();};
this.state=function(){return _state;};
this.status=function(){return _status;};
this.destory=function(){_self.abort();delete(_httpRequest);};
this.alertf=function(error){if(myAjaxConfig.errorReport){alert(error);}};
}
if(!browser){
var browser={};
browser.IE = browser.ie = window.navigator.userAgent.indexOf("MSIE")>0;
browser.Firefox = browser.firefox = browser.FF = browser.MF = navigator.userAgent.indexOf("Firefox")>0;
browser.Gecko = browser.gecko = navigator.userAgent.indexOf("Gecko")>0;
browser.Safari = browser.safari=navigator.userAgent.indexOf("Safari")>0;
browser.Camino = browser.camino=navigator.userAgent.indexOf("Camino")>0;
browser.Opera = browser.opera=navigator.userAgent.indexOf("Opera")>0;
browser.other = browser.OT=!(browser.IE || browser.FF || browser.Safari || browser.Camino || browser.Opera);
}