JS implementation Ajax Request

Source: Internet
Author: User

Excerpt from: http://www.myexception.cn/javascript/1448345.html

AJAX: Full Name "Asynchronous JavaScript and XML" (Asynchronous JavaScript and XML)

Features of Ajax requests:

1. Do not refresh the page

2. The server only returns the required data

Ajax Engine: XMLHttpRequest This object is a built-in object in the browser and is supported in all current browsers.

Get this object in IE:

if (window. ActiveXObject) {
var xmlhttp=new activexobject ("Microsoft.XMLHTTP");
}

Get secondary object in non-ie: var xmlhttp=new xmlhttprequest ();

Compatibility mode:

Var xmlhttp://declares an object

function Creatxmlhttprequest () {
if (window. ActiveXObject) {...} else{//non-IE uses the way ...}

}

requested format: Xmlhttp.open (Request method, request address, [whether asynchronous request]); Xmlhttp.send (); Get Request: Xmlhttp.open ("GET","Ajax.do?name=tom",true); Xmlhttp.send (); Note: The GET request is sent in the same way as the request address followed by the? key=value POST request: Xmlhttp.open ("POST","ajax.do",true); Xmlhttp.setrequestheader ("Content-type","application/x-www-form-urlencoded"); Xmlhttp.send ("Name=tom"); Note: The POST request is passed as a value in the Send () method, with multiple values separated by commas: name=tom,age= AsetRequestHeader means that the type of the request header is set to the form type: Content-type="application/x-www-form-urlencoded"is the default property of form form the process of a complete Ajax request:1, create a XMLHttpRequest object2, call Xmlhttp.open () to set the request content3, set the callback function (what to do based on the status information returned by the server)4, call Xmlhttp.send () to send the request function Sendajax () {createxmlhttprequest ();//invoke the compatibility mode set aboveXmlhttp.open ("GET","Ajax.do?name=tom",true); Xmlhttp.setrequestheader ("Content-type","application/x-www-form-urlencoded"); Xmlhttp.onreadystatechange=callback;//state information changed call functionxmlhttp.send ();}//callback functionfunction callback () {alert ("Callback");} Sendajax ();//The browser will pop up several times (different browsers may be different)Get the status code of the Ajax request and the status code of the HTTP protocol: the status code for the AJAX request has four values:varXmlstate=xmlhttp.readystate; 1, initialized2, in data transmission3, in response to data transfer4, the HTTP protocol has a number of status codes in response:varHttpstate=xmlhttp.status; The common status code is as follows: status code meaning $the server correctly processes the request and responds404The requested page was not found403page without permission to access the request405page does not receive this request method408Request timed out -An exception occurred when the server processed the request503Service is temporarily unavailable304Web page not modified use servlet to get server-side text://ajax.do//This is the Doget method in the Servlet class Public voiddoget (httpservletrequest request,httpservletresponse response) {PrintWriter out=Response.getwriter ();  out. Print ("OK");  out. Flush ();  out. Close ();} //Callbackfunction Callback () {if(xmlhttp.readystate==4){//after the response is complete        if(xmlhttp.status== $){//when the HTTP status code is 200            varResult=xmlhttp.responsetext;//get the text content of an AJAX requestalert (result); }}} Summary: Ajax requests using XMLHttpRequest object IE and non-IE get objects in different ways get, The value of the POST request is the process of the AJAX request, the status code of the AJAX request, and the status code returned by the server in the HTTP protocol gets the text information returned simply by the server

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.