Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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));
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #javascript #xmlhttprequest
ADD COMMENT
Topic
Name
6+5 =