Ajax supports two data submission methods: Get and post. The post method can transmit data larger than 2 K. The differences in Ajax applications are: "the request address of the POST method and the transmitted data are put in two objects-the request address is put in the open object, and the transmitted data is placed in the send object; before transmitting data, define a transmission file HTTP header (setRequestHeader )"
Reference content is as follows:
<Script language = "JavaScript">
Function saveuserinfo (){
// Obtain the accept response information layer
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;
// URL of the received form
VaR url = "Commit. php ";
// The post value is required to connect each variable through &
VaR poststr = "name =" + username + "& age =" + userage + "& sex =" + usersex;
// Instantiate Ajax
VaR Ajax = NULL;
If (window. XMLHttpRequest ){
Ajax = new XMLHttpRequest ();
}
Else if (window. activexobject ){
Ajax = new activexobject ("Microsoft. XMLHTTP ");
}
Else {
Return;
}
// Open the connection through post
Ajax. Open ("Post", URL, true );
// Define the HTTP header information of the transmitted File
Ajax. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
// Send post data
Ajax. Send (poststr );
// The processing function of the returned data
Ajax. onreadystatechange = function (){
If (Ajax. readystate = 4 & Ajax. Status = 200 ){
MSG. innerhtml = Ajax. responsetext;
}
}
}
</SCRIPT>
<Form name = "user_info">
Name: <input type = "text" name = "user_name"/> <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>
// Construct a layer for receiving returned information:
<Div id = "MSG"> </div>