Select to view content in your preferred language

REST endpoint CORS enabled test?

13398
3
Jump to solution
01-13-2014 01:52 PM
DaveHighness
Deactivated User
What I would like to do it have a test for determining if a REST endpoint is CORS enabled, if it is then add the domain to the corsEnabledServer array and if not then do something else, like use the proxy page. Does anyone know of a good function for testing for CORS? It seems like this should be a method in the ArcGIS Javascript API.

Thanks, Dave
0 Kudos
1 Solution

Accepted Solutions
DaveHighness
Deactivated User

I used the javascript found at test-cors.org and built this little test page. Hopefully this will do. I'm not sure yet if it captures every possibility but it will work as a start. Does anyone have anything more elegant?

 <html>
 <head>
     <title>Test for CORS</title>
     <script type="text/javascript">
         //Modified from the code found at test-cors.org
         function testcors(url){
             var createCORSRequest = function(method, url) {
                 var xhr = new XMLHttpRequest();
                 if ("withCredentials" in xhr) {
                     // Most browsers.
                     xhr.open(method, url, true);
                 } else if (typeof XDomainRequest != "undefined") {
                     // IE8 & IE9
                     xhr = new XDomainRequest();
                     xhr.open(method, url);
                 } else {
                     // CORS not supported.
                     xhr = null;
                 }
                 return xhr;
             };
              var method = 'GET';
             var xhr = createCORSRequest(method, url);
              xhr.onload = function() {
                 // Success code goes here.
                 thendothis(true);
             };
              xhr.onerror = function() {
                 // Error code goes here.
                 thendothis(false);
             };
              xhr.send();
             //return iscors;
         }
          function thendothis(iscors){
             var resptxt = "No";
             if (iscors) { resptxt = "Yes"; }
             document.getElementById("res").innerHTML = resptxt;
         }
          function runtest(frm){
             testcors(frm.url.value);
         }
     </script>
 </head>
 <body>
     <form>
         URL: <input type="text" name="url" id="url" style="width:500px" /></br>         <input type="button" value="Test if CORS" onclick="runtest(this.form)" />
     </form>
     <div>URL is CORS: <span id="res"></span></div>
 </body>
 </html> 

View solution in original post

0 Kudos
3 Replies
DaveHighness
Deactivated User
I've found these. Is there anything else out there?

test-cors.org
http://client.cors-api.appspot.com/client
https://github.com/rapportive-oss/cors-test

Dave
0 Kudos
DaveHighness
Deactivated User

I used the javascript found at test-cors.org and built this little test page. Hopefully this will do. I'm not sure yet if it captures every possibility but it will work as a start. Does anyone have anything more elegant?

 <html>
 <head>
     <title>Test for CORS</title>
     <script type="text/javascript">
         //Modified from the code found at test-cors.org
         function testcors(url){
             var createCORSRequest = function(method, url) {
                 var xhr = new XMLHttpRequest();
                 if ("withCredentials" in xhr) {
                     // Most browsers.
                     xhr.open(method, url, true);
                 } else if (typeof XDomainRequest != "undefined") {
                     // IE8 & IE9
                     xhr = new XDomainRequest();
                     xhr.open(method, url);
                 } else {
                     // CORS not supported.
                     xhr = null;
                 }
                 return xhr;
             };
              var method = 'GET';
             var xhr = createCORSRequest(method, url);
              xhr.onload = function() {
                 // Success code goes here.
                 thendothis(true);
             };
              xhr.onerror = function() {
                 // Error code goes here.
                 thendothis(false);
             };
              xhr.send();
             //return iscors;
         }
          function thendothis(iscors){
             var resptxt = "No";
             if (iscors) { resptxt = "Yes"; }
             document.getElementById("res").innerHTML = resptxt;
         }
          function runtest(frm){
             testcors(frm.url.value);
         }
     </script>
 </head>
 <body>
     <form>
         URL: <input type="text" name="url" id="url" style="width:500px" /></br>         <input type="button" value="Test if CORS" onclick="runtest(this.form)" />
     </form>
     <div>URL is CORS: <span id="res"></span></div>
 </body>
 </html> 
0 Kudos
JeffJacobson
Frequent Contributor
0 Kudos