4 <title>CORS test
</title>
6 html,body{font:normal
1em arial,helvetica;}
9 <script> // http://www.html5rocks.com/en/tutorials/cors/
11 // Create the XHR object.
12 function createCORSRequest(method, url) {
13 var xhr = new XMLHttpRequest();
14 if (
"withCredentials" in xhr) {
15 // XHR for Chrome/Firefox/Opera/Safari.
16 xhr.open(method, url, true);
17 } else if (typeof XDomainRequest !=
"undefined") {
18 // XDomainRequest for IE.
19 xhr = new XDomainRequest();
20 xhr.open(method, url);
22 // CORS not supported.
28 // Helper method to parse the title tag from the response.
29 function getTitle(text) {
30 return text.match('
<title>(.*)?
</title>')[
1];
33 // Make the actual CORS request.
34 function makeCorsRequest(method, resource) {
35 var url =
"http://localhost:8080/cors.reply." + resource;
36 var xhr = createCORSRequest(method, url);
38 alert('ERROR: CORS not supported');
43 xhr.onload = function() {
44 var text = xhr.responseText;
45 var title = getTitle(text);
46 alert('Response from CORS request to ' + url + ':\n' + title);
49 xhr.onerror = function() {
50 alert('ERROR: the request failed.');
57 var el = document.getElementById(
"from");
58 el.innerHTML =
"Test CORS from " + document.URL +
" to http://localhost:8080/cors.reply.*";
59 if ((document.URL.indexOf(
"localhost")
>=
0) || (document.URL.indexOf(
"127.0.0.1")
>=
0)) {
60 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.");
66 <body onload=
"start()">
67 <h1>Cross-origin resource sharing test
</h1>
68 <p id=
"from">*** Error: Javascript is not activated. This test will not work. ***
</p>
69 <button onclick=
"makeCorsRequest('GET', 'html')">Run CORS GET request (static resource)
</button>
70 <button onclick=
"makeCorsRequest('GET', 'shtml')">Run CORS GET request (ssi)
</button>
71 <button onclick=
"makeCorsRequest('GET', 'lua/getit')">Run CORS GET request (dynamic resource)
</button>
72 <button onclick=
"makeCorsRequest('PUT', 'lua/putit')">Run CORS PUT request (dynamic resource)
</button>
73 <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>