Custom send requests and process response objects in Ajax

Source: Internet
Author: User

Custom send requests and process response objects in AjaxJavaScript contains an object called XMLHttpRequest that initiates an AJAX request and processes the AJAX response. This object is very complex and contains many features and methods that support Ajax. readyState: Status code for Request "0 (not started), 1 (ON), 2 (Delivered), 3 (in receive), 4 (loaded)"status:http Request status Code "404 (File Not found), + (OK)"onreadystatechange: A function reference that is called when the request state changes, the function event handler is where the response is handled. ResponseText: The response data returned by the server, in the form of a plain text string. Responsexml: The response data returned by the server, in the form of an XML node tree object. abort (): Cancels the request. Open (): Prepares the request, specifying the type of request, URL, and other details. send (): Sends the request to the server for processing. even the most basic AJAX requests require a lot of JavaScript code, because some browsers are inconsistent.
var request=null;if (window. XMLHttpRequest) {try{request=new XMLHttpRequest ();} catch (e) {request=null;}} else if (window. ActiveXObject) {try{request=new ActiveXObject ("Msxm12.xmlhttp");} catch (e) {try{request=new ActiveXObject ("Microsoft.XMLHTTP");} catch (e) {request=null;}}}
After creating the XMLHttpRequest object, switch to the function that sets the processing request, and then turn on the request.Request.onreadystatechange=handler; Custom function called when the server responds to our requestRequest.open (type,url,true); Open the request, prepare the request for delivery, and decide that the get or post,true decision is asynchronousget words:;Request.open ("GET", "Blog.xml", true);request.send (null);Post words:Request.open ("POST", "addblogentry.php", true);Request.setrequestheader ("Content-type", "Application/x-www-form-urlencoded;charset=utf-8");request.send ("09/26/2008&these Dreams Just....&cudeapart.png");
The custom ajaxrequest object eases the painful process of making AJAX requests:featuresRequest: The underlying XMLHttpRequest object is stored in this feature. Methodgetreadystate ();getStatus ();GetResponseText ();getresponsexml ();Send (); A real laborer in ajaxrequest that handles the details of opening and transmitting requests
Ajaxrequest.prototype.send=function (Type,url,handler,postdatatype,postdata) {if (this.request!=null) {//kill the Previous requestthis.request.abort (); try{this.request.onreadystatechange=handler;   Custom processor function This.request.open (type,url,true); if (type.tolowercase () = = "Get") {     //Send GET request this.request.send (null );} else{                      //Send POST request This.request.setRequestHeader ("Content-type", Postdatatype); This.request.send (postdata);}} catch (E) {alert ("Ajax error communicating with the server.\n" + "Details:" +e);}} var ajaxreq=new ajaxrequest (); Ajaxreq.send ("GET", "Movies.xml", handlerequest);



Custom send requests and process response objects in Ajax

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.