Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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

// 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

PREVIOUS NEXT
Code Example
Javascript :: numero random en js 
Javascript :: convert array to csv javascript 
Javascript :: angular input 
Javascript :: how to get the difference between two arrays in javascript 
Javascript :: how to remove last element from array in javascript 
Javascript :: The slice JavaScript string method 
Javascript :: Documenting inside javascript 
Javascript :: [Homepage] is not a <Route component. All component children of <Routes must be a <Route or <React.Fragment 
Javascript :: singleton function javascript 
Javascript :: multipart/form-data ajax jquery 
Javascript :: javascript first letter uppercase 
Javascript :: regex char or char 
Javascript :: Expected the depth of nested jsx elements to be <= 2, but found 3 
Javascript :: React native calender date picker 
Javascript :: range of numbers in javascript 
Javascript :: adding element to array javascript 
Javascript :: chrome block javascript alert 
Javascript :: responseText js 
Javascript :: leaflet dark mode 
Javascript :: swr npm 
Javascript :: random number generatoe js 
Javascript :: convert a string array into object using kerys 
Javascript :: array of obj to obj with reduce 
Javascript :: check if array is empty javascript 
Javascript :: react video 
Javascript :: javascript string new line 
Javascript :: javascript some method 
Javascript :: vue 3 custom input component 
Javascript :: discord.js set role permissions for all channels 
Javascript :: javascript best way to loop through array 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =