Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is XMLHttpRequest like in AJAX?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you what XMLHttpRequest in AJAX is like, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Before you can use a XMLHttpRequest object to send a request and process a response, you must first create a XMLHttpRequest object with JavaScript. Because XMLHttpRequest is not a W3C standard, there are several ways to use JavaScript to create instances of XMLHttpRequest. Internet Explorer implements XMLHttpRequest as an ActiveX object, and other browsers (such as Firefox, Safari, and Opera) implement it as a native JavaScript object. Because of these differences, the logic must be included in the JavaScript code to create an instance of XMLHttpRequest using either ActiveX technology or native JavaScript object technology.

Many people may remember the days when the implementations of JavaScript and DOM on different browsers were very different, and these people might shudder again after hearing this. Fortunately, in order to figure out how to create an instance of a XMLHttpRequest object here, you don't need to write that detailed code to distinguish the browser type. All you have to do is check whether the browser provides support for ActiveX objects. If the browser supports ActiveX objects, you can use ActiveX to create XMLHttpRequest objects. Otherwise, it will be created using native JavaScript object technology. Listing 2-1 shows how easy it is to write cross-browser JavaScript code to create XMLHttpRequest object instances.

Listing 2-1 creates 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 a XMLHttpRequest object is fairly easy. First, create a global scope variable xmlHttp to hold a reference to this object. The createXMLHttpRequest method completes the specific work of creating a XMLHttpRequest instance. In this method, there is only simple branching logic (selection logic) to determine how to create the object. The call to window.ActiveXObject returns an object, or the return null,if statement treats the result returned by the call as true or false (true if the object is returned, false if null is returned), indicating whether the browser supports the ActiveX control and whether the browser is Internet Explorer accordingly. If so, create the XMLHttpRequest object by instantiating a new instance of ActiveXObject and passing in a string indicating what type of ActiveX object to create. In this example, the string supplied to the constructor is Microsoft.XMLHTTP, which indicates that you want to create an instance of XMLHttpRequest.

If the window.ActiveXObject call fails (returning null), JavaScript goes to the else statement branch to determine whether the browser implements XMLHttpRequest as a local JavaScript object. If there is window.

XMLHttpRequest, an instance of XMLHttpRequest is created.

Because JavaScript is dynamically typed and the implementation of XMLHttpRequest is compatible on different browsers, you can access the properties and methods of an XMLHttpRequest instance in the same way, regardless of the method that the instance was created. This greatly simplifies the development process and eliminates the need to write browser-specific logic in JavaScript.

The above is what XMLHttpRequest is like in AJAX. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report