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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
<!DOCTYPE html>
<html>
<head>
<title>CORS test</title>
<style>
html,body{font:normal 1em arial,helvetica;}
</style>
<script> // http://www.html5rocks.com/en/tutorials/cors/
// Create the XHR object.
function createCORSRequest(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.
function getTitle(text) {
return text.match('<title>(.*)?</title>')[1];
}
// Make the actual CORS request.
function makeCorsRequest(method, resource) {
var url = "http://localhost:8080/cors.reply." + resource;
var xhr = createCORSRequest(method, url);
if (!xhr) {
alert('ERROR: CORS not supported');
return;
}
// Response handlers.
xhr.onload = function() {
var text = xhr.responseText;
var title = getTitle(text);
alert('Response from CORS request to ' + url + ':\n' + title);
};
xhr.onerror = function() {
alert('ERROR: the request failed.');
};
xhr.send();
}
function start() {
var el = document.getElementById("from");
el.innerHTML = "Test CORS from " + document.URL + " to http://localhost:8080/cors.reply.*";
if ((document.URL.indexOf("localhost") >= 0) || (document.URL.indexOf("127.0.0.1") >= 0)) {
alert("This CORS test is only meaningful, if you open this site with a different url than \'localhost\' (127.0.0.1).\nYou may use a different IP of the same machine.");
}
}
</script>
</head>
<body onload="start()">
<h1>Cross-origin resource sharing test</h1>
<p id="from">*** Error: Javascript is not activated. This test will not work. ***</p>
<button onclick="makeCorsRequest('GET', 'html')">Run CORS GET request (static resource)</button>
<button onclick="makeCorsRequest('GET', 'shtml')">Run CORS GET request (ssi)</button>
<button onclick="makeCorsRequest('GET', 'lua/getit')">Run CORS GET request (dynamic resource)</button>
<button onclick="makeCorsRequest('PUT', 'lua/putit')">Run CORS PUT request (dynamic resource)</button>
<p>More information on CORS: See <a href="http://enable-cors.org/">enable-cors.org</a> and <a href="http://www.html5rocks.com/en/tutorials/cors/">html5rocks.com</a>.</p>
</body>
</html>
|