10 examples of 'xhr request javascript' in JavaScript

Every line of 'xhr request javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
119function XHRGet(url, callback) {
120 var self = this;
121 var xhr = new XMLHttpRequest();
122 xhr.open("GET", url, true);
123 xhr.send();
124
125 xhr.onreadystatechange = function() {
126 if (xhr.readyState == 4) {
127 if (xhr.status != 200) {
128 console.log("XHR error getting url " + url + ", error: " + xhr.status);
129 }
130 callback(xhr.responseText);
131 }
132 }
133}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

1function xhrRequest(url, callback) {
2 var xhr = new XMLHttpRequest();
3 xhr.open('GET', url, false);
4 xhr.send();
5 callback(xhr.status, xhr.statusText, xhr.responseText);
6}
21function loadXHR(url) {
22 return new Promise(function(resolve, reject) {
23 try {
24 var xhr = new XMLHttpRequest();
25 xhr.open('GET', url);
26 xhr.responseType = 'json';
27 xhr.onerror = function() {reject('Network error.');};
28 xhr.onload = function() {
29 if (xhr.status === 200) {resolve(xhr.response);}
30 else {reject('Loading error:' + xhr.statusText);}
31 };
32 xhr.send();
33 }
34 catch(err) {reject(err.message);}
35 });
36}
100public static get(url, payload: IStringMap = null, headers: IStringMap = null, options = {}): Promise<any> {
101 return XhrRequest.ajax(XhrRequest.Method.GET, url, payload, headers, options);
102}
125function xhr(url, callback) {
126 var r = global.ActiveXObject ?
127 new global.ActiveXObject("Microsoft.XMLHTTP") :
128 new global.XMLHttpRequest()
129
130 r.open("GET", url, true)
131
132 r.onreadystatechange = function() {
133 if (r.readyState === 4) {
134 // Support local file
135 if (r.status > 399 && r.status < 600) {
136 throw new Error("Could not load: " + url + ", status = " + r.status)
137 }
138 else {
139 callback(r.responseText)
140 }
141 }
142 }
143
144 return r.send(null)
145}
157function callRequest(url) {
158 var xmlhr;
159
160 if (window.XMLHttpRequest) {
161 xmlhr = new XMLHttpRequest();
162 } else {
163 xmlhr = new ActiveXObject("Microsoft.XMLHTTP");
164 }
165
166 if(url === segmentSource)
167 {
168 xmlhr.open("GET", url, true);
169 xmlhr.responseType = "arraybuffer";
170 xmlhr.onload = function(){
171 //debugger;
172 if (xmlhr.readyState === 4) return xmlhr;
173 };
174 }
175 else
176 {
177 xmlhr.open("GET", url, false);
178 }
179 xmlhr.send();
180 return xmlhr;
181 }
3function ajaxGet(url, callback) {
4 var req = new XMLHttpRequest();
5 req.open("GET", url);
6 req.addEventListener("load", function () {
7 if (req.status >= 200 && req.status < 400) {
8 // Appelle la fonction callback en lui passant la réponse de la requête
9 callback(req.responseText);
10 } else {
11 console.error(req.status + " " + req.statusText + " " + url);
12 }
13 });
14 req.addEventListener("error", function () {
15 console.error("Erreur réseau avec l'URL " + url);
16 });
17 req.send(null);
18}
9function xhrGet(url, options) {
10 const xhr = new XMLHttpRequest();
11
12 xhr.onload = function() {
13 if (xhr.status >= 200 && xhr.status < 400) {
14 options.success(xhr.responseText, xhr.responseURL);
15 options.complete();
16 } else {
17 options.error();
18 options.complete();
19 }
20 };
21
22 xhr.onerror = function() {
23 options.error();
24 options.complete();
25 };
26
27 xhr.open('GET', url, true);
28 try {
29 xhr.send();
30 } catch (err) {
31 options.error();
32 options.complete();
33 }
34}
2109function createXhr(id) {
2110 var xhr = new XMLHttpRequest();
2111
2112 fileState[id].xhr = xhr;
2113
2114 return xhr;
2115}
1function get(url, responseType, fn) {
2 var xhr = new XMLHttpRequest();
3 xhr.open('GET', url, true);
4 xhr.setRequestHeader('Authorization', 'Basic ZmVlZHRlc3Q6YWJjMTIz');
5 xhr.responseType = responseType;
6 xhr.onload = function() {
7 fn(xhr.response);
8 };
9 xhr.send();
10}

Related snippets