Ajax return garbled Solution

Source: Internet
Author: User
1. vbs conversion functions
The vbs conversion function:
Function convertstr (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 returns & CHR (clng (thischarcode) * & h100 + CINT (nextcharcode ))
I = I + 1
End if
Next
Convertstr = strreturn
End Function

For example
// Ajax Processing
VaR http_request = false;
Function send_request (URL, OBJ) {// initialize, specify the processing function, and send the request Function
VaR OBJ = OBJ;
Http_request = false; // start to initialize the XMLHTTPRequest object.
If (window. XMLHttpRequest) {// Mozilla Browser
Http_request = new XMLHttpRequest ();
If (http_request.overridemimetype) {// sets the mime category
Http_request.overridemimetype ("text/html"); // sets gb2312 encoding to identify data.
}
} Else if (window. activexobject) {// IE browser
Try {
Http_request = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
Try {
Http_request = new activexobject ("Microsoft. XMLHTTP ");
} Catch (e ){}
}
}
If (! Http_request) {// exception. An error occurred while creating the object instance.
Window. Alert ("the XMLHTTPRequest object instance cannot be created .");
Return false;
}
Http_request.onreadystatechange = function (){
If (http_request.readystate = 4) {// judge the object status
If (http_request.status = 200) {// The information has been returned successfully. Start to process the information.
Gb2utf8_data = http_request.responsebody;
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 ");
Document. getelementbyid (OBJ). innerhtml = bytes2bstr (http_request.responsebody );
// Alert (bytes2bstr (http_request.responsebody ));
} Else {// The page is abnormal.
Document. getelementbyid (OBJ). innerhtml = "error! Access timeout! "
}
}
}
Http_request.open ("Post", URL, true );
Http_request.send (null );
} // Function for processing the returned information

2. Use the JS Conversion Function gb2utf8:

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

The usage is the same as above, for example:
Document. getelementbyid (OBJ). innerhtml = gb2utf8 (http_request.responsebody );

Related Article

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.