1var xhttp = new XMLHttpRequest();
2xhttp.onreadystatechange = function() {
3 if (this.readyState == 4 && this.status == 200) {
4 // Typical action to be performed when the document is ready:
5 document.getElementById("demo").innerHTML = xhttp.responseText;
6 }
7};
8xhttp.open("GET", "filename", true);
9xhttp.send();
1function reqListener () {
2 console.log(this.responseText);
3}
4
5var oReq = new XMLHttpRequest();
6oReq.onload = reqListener;
7oReq.open("GET", "http://www.example.org/example.txt");
8oReq.send();
1window.onload = function(){
2 var request = new XMLHttpRequest();
3 var params = "UID=CORS&name=CORS";
4
5 request.onreadystatechange = function() {
6 if (this.readyState == 4 && this.status == 200) {
7 console.log(this.responseText);
8 }
9 };
10
11 request.open('POST', 'https://www.example.com/api/createUser', true);
12 request.setRequestHeader('api-key', 'your-api-key');
13 request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
14 request.send(params);
15}
16
1function httpGet(theUrl)
2{
3 var xmlHttp = new XMLHttpRequest();
4 xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
5 xmlHttp.send( null );
6 return xmlHttp.responseText;
7}
1var url = "https://example.com/";
2
3var xhr = new XMLHttpRequest();
4xhr.open("GET", url);
5
6xhr.onreadystatechange = function () {
7 if (xhr.readyState === 4) {
8 console.log(xhr.status);
9 console.log(xhr.responseText);
10 }};
11
12xhr.send();
1function httpGetAsync(url, callback) {
2 var xmlHttp = new XMLHttpRequest();
3 xmlHttp.onreadystatechange = function() {
4 if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
5 callback(xmlHttp.responseText);
6 }
7 xmlHttp.open("GET", url, true); // true for asynchronous
8 xmlHttp.send(null);
9}