10 examples of 'xmlhttprequest send' in JavaScript

Every line of 'xmlhttprequest send' 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
134send() {
135 this.sending = true;
136 const xhr = this.xhr = new XMLHttpRequest();
137 try {
138 xhr.open(this.type, this.url, true, this.username, this.password);
139 } catch (ieOpenError) {
140 // IE:地址错误时可能产生异常。
141 this.progress(ieOpenError, -3);
142 return false;
143 }
144
145 for (const header in this.headers) {
146 try {
147 xhr.setRequestHeader(header, this.headers[header]);
148 } catch (firefoxSetHeaderError) {
149 // FF:跨域时设置头可能产生异常。
150 }
151 }
152 if (this.contentType) {
153 try {
154 xhr.setRequestHeader("Content-Type", this.contentType);
155 } catch (firefoxSetHeaderError) {
156 // FF:跨域时设置头可能产生异常。
157 }
158 }
159 if (this.withCredentials) {
160 xhr.withCredentials = true;
161 }
162 if (this.responseType && (this.responseType === "arraybuffer" || this.responseType === "blob")) {
163 xhr.responseType = this.responseType;
164 }
165
166 try {
167 xhr.onreadystatechange = this.progress;
168 xhr.send(this.data);
169 } catch (sendError) {
170 // 地址错误时会产生异常。
171 this.progress(sendError, -4);
172 return false;
173 }
174 if (this.timeout >= 0) {
175 setTimeout(this.progress, this.timeout, "Timeout", -2);
176 }
177
178 return true;
179}
142function getXMLHttpRequest() {
143 if (global.XMLHttpRequest) {
144 return new global.XMLHttpRequest;
145 } else {
146 try {
147 return new global.ActiveXObject('Microsoft.XMLHTTP');
148 } catch (e) {
149 throw new Error('XMLHttpRequest is not supported by your browser');
150 }
151 }
152}
72send(data) {
73 debug(`Sending request data: ${data}`);
74 if (this.sandbox) this.sandbox.add(this);
75 let request = this.request;
76 request.send(data);
77 return new Promise(function(resolve, reject) {
78 request.onreadystatechange = function() {
79 if (request.readyState !== 4 /* done */) {
80 return;
81 }
82
83 if (request.status < 200 || request.status >= 400) {
84 return reject(new Error(`Bad status: ${request.status}`));
85 }
86
87 return resolve(request.responseText);
88 };
89
90 request.ontimeout = function() {
91 reject(new Error(`Request timed out after ${request.timeout} ms`));
92 };
93 });
94}
296_startXhr(method, url, headers, body) {
297 var xhr = new global.XMLHttpRequest();
298
299 // If header has Authorization, and cors is available, then set the
300 // withCredentials flag.
301 if (this.withCredentials && corsAvailable) {
302 xhr.withCredentials = true;
303 }
304
305 var self = this;
306 xhr.onreadystatechange = function () { self._xhrCallback(); };
307 xhr.open(method, url, true);
308
309 for (var key in headers) {
310 if (headers.hasOwnProperty(key)) {
311 xhr.setRequestHeader(key, headers[key]);
312 }
313 }
314
315 xhr.send(body);
316
317 consoleUtilities.log("info", "XHR start " + url);
318
319 return xhr;
320}
41send(){
42 var uri = this.uri + (this.uri.indexOf('?') >= 0 ? '&' : '?') + this.callbackParameterName + '=' + this.callbackName;
43
44 window[this.callbackName] = (data) => {
45 delete window[this.callbackName];
46 document.body.removeChild(script);
47
48 if(this.status === undefined){
49 this.status = 200;
50 this.statusText = 'OK';
51 this.response = data;
52 this["onload"](this);
53 }
54 };
55
56 var script = document.createElement('script');
57 script.src = uri;
58 document.body.appendChild(script);
59
60 if(this.timeout !== undefined){
61 setTimeout(() => {
62 if(this.status === undefined){
63 this.status = 0;
64 this["ontimeout"](new Error('timeout'));
65 }
66 }, this.timeout);
67 }
68}
4function execXMLHttpRequest()
5{
6 xhr = new XMLHttpRequest();
7 xhr.open("GET",document.getElementById("url").value,false);
8 xhr.setRequestHeader('My-Custom-Header', 'Some Value');
9 xhr.send();
10 document.getElementById('ta').value = "Status Code: "+xhr.status+"\n\n"+xhr.responseText;
11}
3function xhr(method, url, data, callback){
4 var x = new XMLHttpRequest();
5 x.onreadystatechange = function(){
6 if(x.readyState == 4){
7 if(x.status == 200 || x.status == 204){
8 callback(x.responseText);
9 }else{
10 console.error(method, url, x.status, x.statusText, x.responseText);
11 }
12 }
13 };
14 x.open(method, url);
15 x.send(data);
16}
7function getXHR() {
8 if (window.XMLHttpRequest
9 && ('file:' !== window.location.protocol || !window.ActiveXObject)) {
10 return new XMLHttpRequest();
11 } else {
12 try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch(e) {}
13 try { return new ActiveXObject('Msxml2.XMLHTTP.6.0'); } catch(e1) {}
14 try { return new ActiveXObject('Msxml2.XMLHTTP.3.0'); } catch(e2) {}
15 try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch(e3) {}
16 }
17 return false;
18}
31function createXHR() {
32 var xhr;
33
34 if (typeof XMLHttpRequest !== 'undefined') {
35 xhr = new XMLHttpRequest();
36 }
37 else if (typeof window.ActiveXObject !== 'undefined') {
38 var progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
39 for (var i = 0; i < 3; i += 1) {
40 try {
41 xhr = new window.ActiveXObject(progIds[i]);
42 break;
43 } catch (e) {}
44 }
45 }
46 return xhr;
47}
119export function createXHR() {
120 if (window.XMLHttpRequest) {
121 return new XMLHttpRequest();
122 }
123 return new window.ActiveXObject("Microsoft.XMLHTTP");
124}

Related snippets