Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/core/main/client/net/cors.js
1154 views
1
/**
2
* @namespace beef.net.cors
3
*/
4
5
beef.net.cors = {
6
7
handler: "cors",
8
9
/**
10
* Response Object - used in the beef.net.request callback
11
*/
12
response:function () {
13
this.status = null; // 500, 404, 200, 302, etc
14
this.headers = null; // full response headers
15
this.body = null; // full response body
16
},
17
18
/**
19
* Make a cross-origin request using CORS
20
*
21
* @param method {String} HTTP verb ('GET', 'POST', 'DELETE', etc.)
22
* @param url {String} url
23
* @param data {String} request body
24
* @param timeout {Integer} request timeout in milliseconds
25
* @param callback {Function} function to callback on completion
26
*/
27
request: function(method, url, data, timeout, callback) {
28
29
var xhr;
30
var response = new this.response;
31
32
if (XMLHttpRequest) {
33
xhr = new XMLHttpRequest();
34
35
if ('withCredentials' in xhr) {
36
xhr.open(method, url, true);
37
xhr.timeout = parseInt(timeout, 10);
38
xhr.onerror = function() {
39
};
40
xhr.onreadystatechange = function() {
41
if (xhr.readyState === 4) {
42
response.headers = this.getAllResponseHeaders()
43
response.body = this.responseText;
44
response.status = this.status;
45
if (!!callback) {
46
if (!!response) {
47
callback(response);
48
} else {
49
callback('ERROR: No Response. CORS requests may be denied for this resource.')
50
}
51
}
52
}
53
};
54
xhr.send(data);
55
}
56
} else if (typeof XDomainRequest != "undefined") {
57
xhr = new XDomainRequest();
58
xhr.open(method, url);
59
xhr.onerror = function() {
60
};
61
xhr.onload = function() {
62
response.headers = this.getAllResponseHeaders()
63
response.body = this.responseText;
64
response.status = this.status;
65
if (!!callback) {
66
if (!!response) {
67
callback(response);
68
} else {
69
callback('ERROR: No Response. CORS requests may be denied for this resource.')
70
}
71
}
72
};
73
xhr.send(data);
74
} else {
75
if (!!callback) callback('ERROR: Not Supported. CORS is not supported by the browser. The request was not sent.');
76
}
77
78
}
79
80
};
81
82
beef.regCmp('beef.net.cors');
83
84
85