JavaScript Client to Test CORS: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with " <syntaxhighlight lang='html'> <!DOCTYPE html> <html> <head> <script> var stageUrl = 'https://0o2dx0vi59.execute-api.us-west-2.amazonaws.com/test';...")
 
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Internal=


* [[Cross-Origin_Resource_Sharing#JavaScript_Client_to_Test_CORS|Cross-Origin Resource Sharing]]
* [[JavaScript#Examples|JavaScript]]
=Example=
<syntaxhighlight lang='html'>
<syntaxhighlight lang='html'>
<!DOCTYPE html>
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<html>
    <head>
<head>
        <script>
    <script>


            var stageUrl = 'https://0o2dx0vi59.execute-api.us-west-2.amazonaws.com/test';
        var stageUrl = 'https://c2shocjc3d.execute-api.us-west-2.amazonaws.com/v17';


            function invokeOptions() {
        function invokeOptions() {


                invoke('OPTIONS');
            invoke('OPTIONS');
            }
        }


            function invokeGet() {
        function invokeGet() {


                invoke('GET');
            invoke('GET');
            }
        }


            function invoke(method) {
        function invoke(method) {


                var url = stageUrl + '/a';
            var url = stageUrl + '/inhabitants';


                var xhr = createCORSRequest(method, url);
            var xhr = createCORSRequest(method, url);


                xhr.withCredentials = false;
            xhr.withCredentials = false;


                xhr.onload = function() {
            xhr.onload = function() {


                    document.getElementById("result").innerHTML = "SUCCESS";
                document.getElementById("result").innerHTML = xhr.responseText;
                };
            };


                xhr.onerror = function() {
            xhr.onerror = function() {


                    document.getElementById("result").innerHTML = "ERROR";
                document.getElementById("result").innerHTML = "ERROR: " + xhr.responseText;
                };
            };


                xhr.send();
            xhr.send();
            }
        }


            var createCORSRequest = function(method, url) {
        var createCORSRequest = function(method, url) {


                var xhr = new XMLHttpRequest();
            var xhr = new XMLHttpRequest();


                if ("withCredentials" in xhr) {
            xhr.overrideMimeType("application/json");


                    xhr.open(method, url, true);
            if ("withCredentials" in xhr) {
                }
                else {


                    // CORS not supported.
                xhr.open(method, url, true);
                    xhr = null;
            }
                }
            else {


                 return xhr;
                 // CORS not supported.
            };
                xhr = null;
            }
 
            return xhr;
        };


     </script>
     </script>
</head>
</head>
    <body>
<body>


        <h1>CORS TEST</h1>
<h1>CORS TEST</h1>


        <button type="button" onclick="invokeOptions()">Invoke OPTIONS</button>
<button type="button" onclick="invokeOptions()">Invoke OPTIONS</button>
        <br>
<br>
        <br>
<br>
        <button type="button" onclick="invokeGet()">Invoke GET</button>
<button type="button" onclick="invokeGet()">Invoke GET</button>


        <p id="result"></p>
<p id="result"></p>


    </body>
</body>
</html>
</html>
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 00:13, 30 March 2019

Internal

Example

<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
    <script>

        var stageUrl = 'https://c2shocjc3d.execute-api.us-west-2.amazonaws.com/v17';

        function invokeOptions() {

            invoke('OPTIONS');
        }

        function invokeGet() {

            invoke('GET');
        }

        function invoke(method) {

            var url = stageUrl + '/inhabitants';

            var xhr = createCORSRequest(method, url);

            xhr.withCredentials = false;

            xhr.onload = function() {

                document.getElementById("result").innerHTML = xhr.responseText;
            };

            xhr.onerror = function() {

                document.getElementById("result").innerHTML = "ERROR: " + xhr.responseText;
            };

            xhr.send();
        }

        var createCORSRequest = function(method, url) {

            var xhr = new XMLHttpRequest();

            xhr.overrideMimeType("application/json");

            if ("withCredentials" in xhr) {

                xhr.open(method, url, true);
            }
            else {

                // CORS not supported.
                xhr = null;
            }

            return xhr;
        };

    </script>
</head>
<body>

<h1>CORS TEST</h1>

<button type="button" onclick="invokeOptions()">Invoke OPTIONS</button>
<br>
<br>
<button type="button" onclick="invokeGet()">Invoke GET</button>

<p id="result"></p>

</body>
</html>