0
Q:

xhttp javascript

// Use these functions:

function _GET_REQUEST(url, response) {
  var xhttp;
  if (window.XMLHttpRequest) {
    xhttp = new XMLHttpRequest();
  } else {
    xhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }

  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      response(this.responseText);
    }
  };

  xhttp.open("GET", url, true);
  xhttp.send();
}

function _POST_REQUEST(url, params, response) {
  var xhttp;
  if (window.XMLHttpRequest) {
    xhttp = new XMLHttpRequest();
  } else {
    xhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }

  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      response(this.responseText);
    }
  };

  xhttp.open("POST", url, true);
  xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhttp.send(params);
}


// Use like:
_GET_REQUEST('http://url.com', (response) => {
	// Do something with variable response
  	console.log(response);
});
_POST_REQUEST('http://url.com', 'parameter=sometext', (response) => {
	// Do something with variable response
  	console.log(response);
});
2

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
 document.getElementById("demo").innerHTML = xhttp.responseText;

    }
};
xhttp.open("GET", "url", true);
xhttp.send();

 
0
XMLHttpRequest. XMLHttpRequest is a built-in browser object that allows to make HTTP requests in JavaScript. Despite of having the word “XML” in its name, it can operate on any data, not only in XML format. We can upload/download files, track progress and much more.
2
<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  
  //looking for a change or state , like a request or get.
  xhttp.onreadystatechange = function() {
     
     //if equal to 4 means that its ready.
    // if equal to 200 indicates that the request has succeeded.
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML = this.responseText;
    }
  };
  
  //GET method for gettin the data // the file you are requesting
  xhttp.open("GET", "TheFileYouWant.html", true);
  
  //sending the request
  xhttp.send();
}
1

New to Communities?

Join the community