Description of Ajax XMLHttpRequest

Source: Internet
Author: User

Before using the XMLHTTPRequest object to send requests and process responses, you must use JavaScript to create an XMLHTTPRequest object. Because XMLHttpRequest is not a W3C standard, you can use JavaScript to create an XMLHttpRequest instance in multiple ways. Internet Explorer implements XMLHttpRequest as an ActiveX object, while other browsers (such as Firefox, Safari, and opera) Implement XMLHttpRequest as a local JavaScript Object. Due to these differences, JavaScript code must contain the relevant logic to use ActiveX technology or local JavaScript Object technology to create an instance of XMLHttpRequest.

Many may remember the days before when JavaScript and Dom implementations on different browsers were quite different. After listening to the above, these people may shudder. Fortunately, to clarify how to create an XMLHTTPRequest object instance, you do not need to write code in detail to differentiate the browser type. All you need to do is check whether the browser supports ActiveX objects. If the browser supports ActiveX objects, you can use ActiveX to create XMLHttpRequest objects. Otherwise, it is necessary to use the local JavaScript Object technology to create. Code List 2-1 shows how easy it is to write cross-browser JavaScript code to create an XMLHTTPRequest object instance.

Code List 2-1 create an instance of the XMLHTTPRequest object

 

VaR XMLHTTP;

Function createxmlhttprequest (){
If (window. activexobject ){
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest ){
XMLHTTP = new XMLHttpRequest ();
}
}

As you can see, creating an XMLHTTPRequest object is quite easy. First, create a global scope variable XMLHTTP to save the reference of this object. The createxmlhttprequest method is used to create an XMLHttpRequest instance. In this method, only simple branch logic (select logic) is used to determine how to create an object. For window. activexobject calls will return an object, or return NULL. If statements will regard the returned results as true or false (if the returned object is true, then null is false ), this indicates whether the browser supports ActiveX controls and whether the browser is Internet Explorer. If yes, create an XMLHTTPRequest object by instantiating a new instance of activexobject, and input a string to indicate the type of ActiveX object to be created. In this example, the string provided for the constructor is Microsoft. XMLHTTP. This indicates that you want to create an instance of XMLHttpRequest.

If window. activexobject fails to be called (null is returned), JavaScript will go to the else statement branch to determine whether the browser implements XMLHttpRequest as a local JavaScript Object. If window exists.

XMLHttpRequest creates an instance of XMLHttpRequest.

Because JavaScript has the dynamic type feature and XMLHttpRequest is compatible with different browsers, you can access the attributes and methods of the XMLHttpRequest instance in the same way, regardless of how the instance is created. This greatly simplifies the development process, and does not have to write browser-specific logic in JavaScript.

Related Article

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.