1<!DOCTYPE html> 2<html> 3<head> 4<title>CORS test</title> 5<style> 6 html,body{font:normal 1em arial,helvetica;} 7</style> 8 9<script> // http://www.html5rocks.com/en/tutorials/cors/ 10 11// Create the XHR object. 12function 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); 21 } else { 22 // CORS not supported. 23 xhr = null; 24 } 25 return xhr; 26} 27 28// Helper method to parse the title tag from the response. 29function getTitle(text) { 30 return text.match('<title>(.*)?</title>')[1]; 31} 32 33// Make the actual CORS request. 34function makeCorsRequest(method, resource) { 35 var url = "http://localhost:8080/cors.reply." + resource; 36 var xhr = createCORSRequest(method, url); 37 if (!xhr) { 38 alert('ERROR: CORS not supported'); 39 return; 40 } 41 42 // Response handlers. 43 xhr.onload = function() { 44 var text = xhr.responseText; 45 var title = getTitle(text); 46 alert('Response from CORS request to ' + url + ':\n' + title); 47 }; 48 49 xhr.onerror = function() { 50 alert('ERROR: the request failed.'); 51 }; 52 53 xhr.send(); 54} 55 56function start() { 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."); 61 } 62} 63</script> 64 65</head> 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> 74</body> 75</html> 76