cors.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. <script> // http://www.html5rocks.com/en/tutorials/cors/
  9. // Create the XHR object.
  10. function createCORSRequest(method, url) {
  11. var xhr = new XMLHttpRequest();
  12. if ("withCredentials" in xhr) {
  13. // XHR for Chrome/Firefox/Opera/Safari.
  14. xhr.open(method, url, true);
  15. } else if (typeof XDomainRequest != "undefined") {
  16. // XDomainRequest for IE.
  17. xhr = new XDomainRequest();
  18. xhr.open(method, url);
  19. } else {
  20. // CORS not supported.
  21. xhr = null;
  22. }
  23. return xhr;
  24. }
  25. // Helper method to parse the title tag from the response.
  26. function getTitle(text) {
  27. return text.match('<title>(.*)?</title>')[1];
  28. }
  29. // Make the actual CORS request.
  30. function makeCorsRequest(method, resource) {
  31. var url = "http://localhost:8080/cors.reply." + resource;
  32. var xhr = createCORSRequest(method, url);
  33. if (!xhr) {
  34. alert('ERROR: CORS not supported');
  35. return;
  36. }
  37. // Response handlers.
  38. xhr.onload = function() {
  39. var text = xhr.responseText;
  40. var title = getTitle(text);
  41. alert('Response from CORS request to ' + url + ':\n' + title);
  42. };
  43. xhr.onerror = function() {
  44. alert('ERROR: the request failed.');
  45. };
  46. xhr.send();
  47. }
  48. function start() {
  49. var el = document.getElementById("from");
  50. el.innerHTML = "Test CORS from " + document.URL + " to http://localhost:8080/cors.reply.*";
  51. if ((document.URL.indexOf("localhost") >= 0) || (document.URL.indexOf("127.0.0.1") >= 0)) {
  52. 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.");
  53. }
  54. }
  55. </script>
  56. </head>
  57. <body onload="start()">
  58. <h1>Cross-origin resource sharing test</h1>
  59. <p id="from">*** Error: Javascript is not activated. This test will not work. ***</p>
  60. <button onclick="makeCorsRequest('GET', 'html')">Run CORS GET request (static resource)</button>
  61. <button onclick="makeCorsRequest('GET', 'shtml')">Run CORS GET request (ssi)</button>
  62. <button onclick="makeCorsRequest('GET', 'lua/getit')">Run CORS GET request (dynamic resource)</button>
  63. <button onclick="makeCorsRequest('PUT', 'lua/putit')">Run CORS PUT request (dynamic resource)</button>
  64. <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>
  65. </body>
  66. </html>