How to solve the garbled Get and post errors when js calls AJAX

Source: Internet
Author: User

When "get" is used, the captured page is appended with the encoding type Copy codeThe Code is as follows: <%
Server: servletactioncontext. getresponse (). setcharacterencoding ("UTF-8 ");
Client webpage special effect p/jsp.html target = _ blank> jsp Tutorial: <% @ page language = "java" contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%>
Response. expires =-9999
Response. addheader "pragma", "no-cache"
Response. addheader "cache-ctrol", "no-cache"
Response. addheader "content-type", "text/html; charset = gb2312" 'is the focus, otherwise garbled
Response. write "Chinese characters" %>

2. vbscript is used to solve the encoding problem when post is used:
The source code is as follows:Copy codeThe Code is as follows: <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
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
</Script>

The following uses the vbscript function:Copy codeThe Code is as follows: <script language = webpage Effect>
/**
* Initialize an xmlhttp object
*/
Function initajax ()
{
Var ajax = false;
Try {
Ajax = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
Try {
Ajax = new activexobject ("microsoft. xmlhttp ");
} Catch (e ){
Ajax = false;
}
}
If (! Ajax & typeof xmlhttprequest! = 'Undefined '){
Ajax = new xmlhttprequest ();
}
Return ajax;
}
Function saveuserinfo ()
{
Var msg = document. getelementbyid ("msg ");
Var f = document. user_info;
Var username = f. user_name.value;
Var userage = f. user_age.value;
Var usersex = f. user_sex.value;
Var url = "save. asp tutorial ";
Var poststr = urlencoding ("user_name =" + username + "& user_age =" + userage + "& user_sex =" + usersex); // use encoding for post
Var ajax = initajax ();
Ajax. open ("post", url, true );
Ajax. setrequestheader ("content-type", "application/x-www-form-urlencoded ");
Ajax. send (poststr );
Ajax. onreadystatechange = function (){
If (ajax. readystate = 4 & ajax. status = 200 ){
Msg. innerhtml = bytes2bstr (ajax. responsebody); // decoded upon retrieval
}
}
}
</Script>
<Form name = "user_info">
Name: <textarea name = "user_name"/> </textarea> <br/>
Age: <input type = "text" name = "user_age"/> <br/>
Gender: <input type = "text" name = "user_sex"/> <br/>
<Input type = "button" value = "Submit Form" onclick = "saveuserinfo ()">
</Form>
<Div id = "msg"> </div>
Successful

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.