Ajax asynchronous interaction, post and get commit differences __ajax

Source: Internet
Author: User

<script type= "Text/javascript" >
var xmlHttp;
/* Create asynchronous object, create by browser difference
function Createxmlhttprequest () {
if (window. Activeobject) {
Xmlhttp=new activeobject ("Microsoft.XMLHTTP");
}else if (window. XMLHttpRequest) {
Xmlhttp=new XMLHttpRequest ();
}
}

ReadyState: Status of the request, 4 for data reception success
HTTP request response value returned by the server, 200 indicates successful request
decodeURI decoding of the returned data
function Handlestatechange () {
if (xmlhttp.readystate==4&&xmlhttp.status==200) {
var Responsediv=document.getelementbyid ("Serverresponse");
Responsediv.innerhtml=decodeuri (Xmlhttp.responsetext);
}
}

Two-encodeURI encoding of sent parameters
function createquerystring () {
var Firstname=document.getelementbyid ("FirstName"). Value;
var Birthday=document.getelementbyid ("Birthday"). Value;
var querystr= "firstname=" +firstname+ "&birthday=" +birthday;
Return encodeURI (encodeURI (QUERYSTR));
}
The method of the GET request, with the requested URL followed by a parameter, and a null value in the SENDD (null) method.
function Dorequestusingget () {
Createxmlhttprequest ();
var querystring= "test.jsp";
Querystring+=createquerystring () + "&timestamp=" +new Date (). GetTime ();
Xmlhttp.onreadystatechange=handlestatechange;
Alert (querystring);
Xmlhttp.open ("Get", querystring);
Xmlhttp.send (NULL);
}
Method of Post request, parameters are passed in the Send method
function Dorequestusingpost () {
Createxmlhttprequest ();
var url= "test.jsp?timestamp=" +new Date (). GetTime ();
var querystring=createquerystring ();
Xmlhttp.open ("post", url);
Xmlhttp.onreadystatechange=handlestatechange;
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlhttp.send (querystring);
}
</script>


<body>
<form action= "" >
<input type= "text" id= "FirstName"/><br/>
<input type= "text" id= "Birthday"/>
</form>
<form action= "" >
<input type= "button" value= "Get" onclick= "Dorequestusingget ()"/><br/>
<input type= "button" value= "POST" onclick= "Dorequestusingpost ()"/>
</form>
<div id= "Serverresponse" ></div>
</body>

The code for the server segment is as follows:

<%
 if (Request.getmethod (). Equals ("POST")) {
  response.getwriter (). Write ("Post submit" +request.getparameter ("FirstName"));
 }else if (Request.getmethod (). Equals ("Get")} {
  response.getwriter (). Write ("Get method Commit" + Request.getparameter ("FirstName"));
 }
%>

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.