The garbled method of Get and post when JS calls Ajax _javascript Tips

Source: Internet
Author: User
Tags chr
When you use "get", the page that you crawl finally adds the encoding type
Copy Code code as follows:

<%
Server side: Servletactioncontext.getresponse (). setcharacterencoding ("Utf-8");
Client page Special effects 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 "' This is the key, otherwise there will be garbled
Response.Write "Chinese Characters"%>

2. Use VBScript to solve the coding problem when using post:
The source code is as follows:
Copy Code code as follows:

<script language= "VBScript" >
function urlencoding (Vstrin)
Strreturn = ""
For i = 1 to Len (Vstrin)
THISCHR = Mid (vstrin,i,1)
If ABS (ASC (THISCHR)) < &AMP;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 is the use of VBScript functions:
Copy Code code as follows:

<script language= Web page Special effects >
/**
* Initialization of a 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);
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); decoding when getting
}
}
}
</script>
<form name= "User_info" >
Name: <textarea name= "user_name"/></textarea><br/>
Age: <input type= "text" name= "User_age"/><br/>
Sex: <input type= "text" name= "User_sex"/><br/>
<input type= "button" value= "Submitting form" onclick= "Saveuserinfo ()" >
</form>
<div id= "MSG" ></div>
Success
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.