Ajax core -- XMLHttpRequest object

Source: Internet
Author: User

XMLHttpRequest objects are the core of AJAX functions. To learn about XMLHttpRequest objects, you must first create XMLHttpRequest objects and learn how to create XMLHttpRequest objects in different browsers using different methods: in the past, I only heard that some Browsers Do not support Ajax. Later, the browser gradually became compatible with this browser. Now I understand a lot. I can see the substantive code based on the Code: let's take a look at how IE creates an XMLHttpRequest object (method 1): [java] view plaincopyprint? <Span style = "font-size: 18px"> var xmlhttp = ActiveXobject ("Msxml12.XMLHTTP"); // create the Msxml12.XMLHTTP object var xmlhttp = ActiveXobject ("Microsoft. XMLHTTP "); // create Microsoft in older IE versions. XMLHTTP object </span> while Mozilla, Opera, Safari, and most non-IE browsers use the following method (method 2) to create the XMLHttpRequest object:

<span style="font-size:18px"> var xmlhttp=new XMLHttpRequest();</span>  

 

Note: In fact, InternetExplorer uses an object named XMLHttp instead of the XMLHttpRequest object. Mozilla, Opera, Safari, and most non-Microsoft browsers use the latter (hereinafter referred to as XMLHttpRequest objects ). IE7 began to use the XMLHttpRequest object. Therefore, we need to create an XMLHTTPRequest object compatible with multiple browsers: the first method:
<Span style = "font-size: 18px"> var xmlhttp = false; // create a new variable and assign a value of false, if false is used as the condition, it means that the XMLHTTPRequest object function CreateXMLHttp () {try {xmlhttp = new XMLHttpRequest () has not been created. // try to create the XMLHttpRequest object, all browsers except IE support this method. } Catch (e) {try {xmlhttp = ActiveXobject ("Msxml12.XMLHTTP"); // use a newer version of IE to create an Internet Explorer-compatible object (Msxml2.XMLHTTP ). } Catch (e) {try {xmlhttp = ActiveXobject ("Microsoft. XMLHTTP"); // use older IE versions to create IE-compatible objects (Microsoft. XMLHTTP ). } Catch (failed) {xmlhttp = false; // if it fails, keep it false }}return xmlhttp;}. Example of success: if (! Xmlhttp) {xmlhttp creation failed} else {xmlhttp creation successful} </span>

 

Method 2:
<span style="font-size:18px">if(typeof(XMLHttpRequest)=="undefined" && window.ActiveXObject){      function XMLHttpRequest(){      var xmlhttp_arr=["MSXML2.XMLHTTP","Microsoft.XMLHTTP"];      var xmlhttp;        for(i=0;i<xmlhttp_arr.length;i++){       if(xmlhttp=new ActiveXObject(xmlhttp_arr[i]))       break;      }      return xmlhttp;      }    }</span>  

 

The following is an xmlhttpRequest object that sends the request data and returns the result. An XMLHTTPRequest object is generated.
<Span style = "font-size: 18px"> var xmlhttp = CreatXMLHttp (); xmlhttp. open ("get", "http://www.blog.sina.com.cn/jaryle", true); xmlhttp. onReadyStateChange = getresult; // how can I tell the XMLHttpRequest object whose status changes to handle this change? Two methods are used: one is the anonymous method xmlhttp. onReadyStateChange = function () {code for processing changes} another method: specify the method: xmlhttp. onReadyStateChange = getresult; function getresult () {code for processing changes} xmlhttp. send (); function getresult () {if (xmlhttp. readyState = 4) {// When readyState is 4, it indicates that if (xmlhttp. status = 200) {// This requires the status attribute, that is, the HTTP status code returned by the server. When xmlhttp. status is 200, it indicates that the transmission process is complete and there is no error alert (xmlhttp. responseText); }}</span>

 

Note: We should follow the above process to remember: Create an XMLHttpRequest object-> specify the Sending address and method-> specify the processing method of status change-> send a request, when the status of the request changes, the specified processing method is automatically called. Next, let's get to know more ......

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.