Ad Code

Ticker

6/recent/ticker-posts

Which of the following syntax of xmlhttprequest object creation is correct for Internet Explorer browser?



The XMLHttpRequest Object
The XMLHttpRequest object used to exchange data between web elements and servers will change data from server to web part without browser refresh or reload.
It makes it faster to the website because it runs after all components loaded into the browser screen. That means if any website change data parts frequently its may usage XMLHttpRequest to get data from the server.

It received data like JSON, XML, HTML and simple text, etc. 

All modern browsers (Chrome, Firefox, IE7+, Edge, Safari, Opera) have a built-in XMLHttpRequest object.

the following method to create XMLHttpRequest.

var xhr = new XMLHttpRequest();
For old browser(IE5 and IE6)
  var xhr = new ActiveXObject("Microsoft.XMLHTTP");
for handling both with the condition for all browsers to make a script inside the condition to check which Http object available in current browsers.
var xhr;
if (window.XMLHttpRequest) {
   // code for modern browsers
   xhr = new XMLHttpRequest();
 } else {
   // code for old IE browsers
   xhr = new ActiveXObject("Microsoft.XMLHTTP");
 }
this example to check if XMLHttpRequest available in the browser then it will create otherwise create ActiveX object for Http requests.

This method is also known as AJAX requests for the modern script library like Jquery.

GET Requests

The get requests use to get data from a specific resource.

var xhr;
  if (window.XMLHttpRequest) {
    // code for modern browsers
    xhr = new XMLHttpRequest();
  } else {
    // code for IE6, IE5
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
  }
  xhr.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     console.log(this.responseText);
    }
  };
  xhr.open("GET", "https://domain/api/list", true);
  xhr.send();
This example gets list data to print inside the console of the browser.
xhr.onreadystatechange:- It runs when requests complete and getting a response here, and if this.readyState == 4 and this.status==200 to insure response was successfully getting on the browser otherwise it gives errors which mapped with else conditions.

Post a Comment

0 Comments

Ad Code