Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

xmlhttprequest javascript

let xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function(){
    if(this.readyState === 4 && this.status === 200)
      {
        document.getElementById('response-div').innerHTML = this.responseText
      }
    }
	//get request with params
    xhr.open('GET', 'example?param1=true&param2=2');
  xhr.send();
});
Comment

js xmlhttprequest get request

// create request
const request = new XMLHttpRequest();
// function to handle result of the request
request.addEventListener("load", function() {
	// if the request succeeded
	if (request.status >= 200 && request.status < 300) {
		// print the response to the request
		console.log(request.response);
    }
});
// open the request
request.open("GET", "someurl");
// send the request
request.send();
Comment

xmlhttprequest javascript

function reqListener () {
  console.log(this.responseText);
}

var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.open("GET", "http://www.example.org/example.txt");
oReq.send();
Comment

xmlhttprequest

// note: following are the shortest examples
// synchronous request, will block main thread
function requestSync(url) {
	var xhr = new XMLHttpRequest();
	xhr.open("GET", url, false);
	xhr.send();
	return xhr.responseText;
};
console.log(requestSync("file.txt"));

// async
function requestAsync(url) {
	return new Promise(function (resolve, reject) {
		var xhr = new XMLHttpRequest();
		xhr.open("GET", url);
		xhr.onload = () => resolve(xhr.response);
		xhr.send();
	});
}
requestAsync("file.txt").then((res) => console.log(res));
Comment

xmlhttprequest js

var url = "https://jsonplaceholder.typicode.com/posts";

var xhr = new XMLHttpRequest();
xhr.open("GET", url);

xhr.onreadystatechange = function () {
	if (xhr.readyState === 4) {
    	console.log(xhr.status);
        console.log(xhr.responseText);
    }
};
xhr.send();
Comment

XMLHttpRequest object

function reqListener () {
  console.log(this.responseText);
}

var oReq = new XMLHttpRequest();
oReq.addEventListener("load", reqListener);
oReq.open("GET", "http://www.example.org/example.txt");
oReq.send();
Comment

javascript xmlhttprequest

// note: following are the shortest examples
// synchronous request, will block main thread
function requestSync(url) {
	var xhr = new XMLHttpRequest();
	xhr.open("GET", url, false);
	xhr.send();
	return xhr.responseText;
};
console.log(requestSync("file.txt"));

// async
function requestAsync(url) {
	return new Promise(function (resolve, reject) {
		var xhr = new XMLHttpRequest();
		xhr.open("GET", url);
		xhr.onload = () => resolve(xhr.response);
		xhr.send();
	});
}
requestAsync("file.txt").then((res) => console.log(res));
Comment

PREVIOUS NEXT
Code Example
Javascript :: swap function javascript 
Javascript :: how to run an existing react project 
Javascript :: placing card on center in angular flex layout 
Javascript :: array iteration 
Javascript :: hide column in antd table using js / react with conditional rendering 
Javascript :: javascript append array to array 
Javascript :: upload excel file using jquery ajax 
Javascript :: disable button using jquery 
Javascript :: multithreading in javascript 
Javascript :: how to change mui ripple color 
Javascript :: Pass Props to a Component Using defaultProps in react 
Javascript :: javascript get black or white text base on color 
Javascript :: a.reduce 
Javascript :: react context 
Javascript :: url validation in formcontrol angular 8 
Javascript :: unix to date in javascript 
Javascript :: merge 2 array of object by key 
Javascript :: javascript Compare two arrays regardless of order 
Javascript :: socket.io cors 
Javascript :: how to remove property of object in javascript without delete 
Javascript :: get localstorage value 
Javascript :: ng model on change 
Javascript :: preventdefault not working form submit react 
Javascript :: jquery fadeout to fadein 
Javascript :: how to create a cookie in javascript 
Javascript :: remove duplicates in an array in javascript 
Javascript :: how to clear an input in testing library 
Javascript :: set datatable with jquery success return value 
Javascript :: in text includes in aray of objects 
Javascript :: useroutematch 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =