On the solution of UTF-8 client using AJAX to get GB2312 server-side garbled problem _javascript tips

Source: Internet
Author: User
The client is UTF-8 encoded, which is now recognized as standard code
In this case, when practical Ajax gets GB2312 encoded server-side information asynchronously, it is unavoidable to encounter Chinese character garbled problem
Because the target data is GB2312, but XMLHttpRequest default is to use UTF-8 for data encapsulation, it will produce garbled
I believe a lot of people are using the lightweight JS toolset-prototype.js, its Ajax function is also excellent
I've also been using it, so it's always been based on prototype.js to consider this problem.
But after many tests, it failed to convert the responsetext it returned to the correct encoding format.
It was later learned that in the Responsebody attribute of the XMLHttpRequest object, the original data information was saved
But the Prototype.js Ajax feature returns the Responsebody attribute is undefined, it seems to be a do-it-yourself
After nearly one hours of banging, a dapper Ajax framework was born, haha, but the function is still very full
Some of these are written to draw on the implementation of another lightweight AJAX framework-bingo.js
Calling methods and Comments:
Copy Code code as follows:

Myajaxcall ({
URL: ' xxxxx.jsp '//Destination page address
, params:urlencoding (' prm1= parameter 1&prm2= parameter 2 ')//parameter string information
, method: ' Post '//Send way post or get
, Callback:retvalue//callback function name
, Isbody:true//whether to return Responsebody, default return responsetext
, Isxml:false//whether to return data in XML format
, Errorreport:false///Send the error, whether pop-up prompts
, Attachs: {}//additional parameters that can be passed to the callback function
});
function RetValue (Res,att) {
var strret = bytes2bstr (res);
alert (strret);
}

Take a look at two of these functions:

, urlencoding: Encoding the parameters
, BYTES2BSTR: Decoding the returned data

These two functions refer directly to the very popular two coding functions on the network, but they are all written in VBS.
You need to attach these two functions to the above page:
Copy Code code as follows:

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

Attached below is the lightweight AJAX framework I wrote-Myajax.js source:
Copy Code code as follows:

/**
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;n<msxml.length;n++) {
try {
_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 ();d elete (_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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.