-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCors.js
More file actions
55 lines (46 loc) · 1.58 KB
/
Cors.js
File metadata and controls
55 lines (46 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Create the XHR object.
var cors = {};
cors = {
createCORSRequest: function (method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
},
// Helper method to parse the title tag from the response.
getTitle: function (text) {
return text.match('<title>(.*)?</title>')[1];
},
// Make the actual CORS request.
makeCorsRequest: function () {
// All HTML5 Rocks properties support CORS.
var url = 'http://unittestjavascriptkata.azurewebsites.net/BinaryKataService.svc';
var url = 'http://api.alice.com/cors';
var xhr = createCORSRequest('GET', url);
xhr.send();
var xhr = cors.createCORSRequest('GET', url);
if (!xhr) {
alert('CORS not supported');
return;
// Response handlers.
xhr.onload = function () {
var text = xhr.responseText;
var title = cors.getTitle(text);
alert('Response from CORS request to ' + url + ': ' + title);
};
xhr.onerror = function () {
alert('Woops, there was an error making the request.');
};
xhr.send();
}
}
}