Create an XMLHttpRequest object and an xmlhttprequest object

Source: Internet
Author: User

Create an XMLHttpRequest object and an xmlhttprequest object

First, we will introduce the XMLHttpRequest object. We all know that Ajax is not a single-gate technology, but a combination of several technologies. XMLHttpRequest is the core technology in the Ajax technology system. Without XMLHttpRequest, other Ajax technologies cannot become an organic whole. In Ajax applications, the XMLHttpRequest object sends user information to the server asynchronously and receives the response and data returned by the server.

XMLHttpRequest is an object of the XMLHttp component. You can use XMLHttpRequest to partially update the webpage information without submitting the entire page to the server. This may be a bit abstract. Everyone often goes to the QQ space to view the news of friends. Some friends share videos, after clicking play, the mouse and keyboard do not lose activity ability during video buffering or during playback. We can continue to browse other dynamics, while sliding the mouse wheel up or down, the video page also follows the current position we browsed, as shown in. This function can be implemented through Ajax.

The XMLHttpRequest object provides a series of attributes and methods to send asynchronous HTTP requests to the server. When the server processes user requests, the XMLHttpRequest object uses the state value of the attribute to reflect the status of the HTTP request in real time and guides javascript to process the request based on the obtained State. In addition, when the server responds to user actions and returns the user, the response series methods provided by XMLHttpRequest can assemble the returned data into javascript scripts provided by the text format, XML format, or unsigned byte data format.

Before using the XMLHttpRequest object to send requests and process responses, you must use javascript to create the XMLHttpRequest object. Because the XMLHttpRequest object is not currently W3C standard, the creation methods in the new version of IE browser, the old version of IE browser, and non-IE browser are also different. Therefore, before creating an XMLHttpRequest object, you must determine the browser type. Microsoft. XMLHTTP is required for earlier ie browsers, while Msxml2.XMLHTTP is required for later ie browsers.

The following code creates an XMLHttpRequest object:

<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> <script language = "javascript" type = "text/javascript"> var xmlHttp; // use the new version of IE to create the XMLHttpRequest object try {xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP");} catch (e) {// create the XMLHttpRequest object try {xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ") ;}catch (E) {}// use another browser to create the XMLHttpRequest object if (! XmlHttp & typeof XMLHttpRequest! = 'Undefined') {try {xmlHttp = new XMLHttpRequest () ;}catch (e) {xmlHttp = false ;}</script> </span>

From the code, we can see that although logical judgment is required, creating an XMLHttpRequest object is not complicated. I did not have a thorough understanding of XMLHttpRequest. I just sorted it out based on the examples in the video and the knowledge in the Ajax Programming Technology textbook. I hope you can exchange and make progress together.


How to create an XMLHttprequest object?

In fact, there are two types of lines. The main difference between the two is the difference between browsers. Different browsers have different methods for declaring XMLHttprequest in different versions. for IE, new ActiveXObject ("MSXML2.XMLHTTP ")
New XMLHttprequest ()

In fact, there are already a lot of ajax scripts based on XMLHttprequest, and there is no need to study such a relatively low-layer thing. You can look at jQuery and the mainstream JS script library, XMLHttprequest () has been encapsulated ()

"You cannot create an XMLHttpRequest object instance"

Your browser does not support XMLHttpRequest and activeXObject that does not support window. Fix your browser, or reinstall IE6.0.

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.