Welcome to FreeSoftwareServers Confluence Wiki

Since CORs is a Browser only issue, best to test via HTML.

Below is a basic HTML file to edit the URL and test CORs.

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta http-equiv="Content-Style-Type" content="text/css">
  <title>CORS Tester</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body>
  <H2>To view test result</H2>
  <h3>Right click this page > Inspect Element > Console </h3>
<p id="out"></p>

  <script>
  console.log("starting ajax request to the resource.");
  $.ajax
      ({type: "get",                     
          contentType: "application/json; charset=UTF-8",
          url: "https://api.github.com",
          headers: {},
          xhrFields: {                  
                withCredentials: false
            },
      success: function(data)
      {
        console.log("API is CORS enabled");
        $('#out').text('API is CORS enabled. Response:' + JSON.stringify(data));
        console.log('Response:')
        console.log(data)
      },
      done: function( jqXHR, textStatus ) {
          console.log( "Request failed: " + textStatus + jqXHR );
      },
    });
  console.log("ajax request sent.");
  </script>
</body>
</html>
  • No labels