JavaScript Client to Test CORS: Difference between revisions
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 |
||
Line 1: | Line 1: | ||
=Internal= | |||
* [[Cross-Origin_Resource_Sharing#JavaScript_Client_to_Test_CORS|Cross-Origin Resource Sharing]] | |||
=Example= | |||
<syntaxhighlight lang='html'> | <syntaxhighlight lang='html'> | ||
<!DOCTYPE html> | <!DOCTYPE html> |
Revision as of 23:53, 29 March 2019
Internal
Example
<!DOCTYPE html>
<html>
<head>
<script>
var stageUrl = 'https://0o2dx0vi59.execute-api.us-west-2.amazonaws.com/test';
function invokeOptions() {
invoke('OPTIONS');
}
function invokeGet() {
invoke('GET');
}
function invoke(method) {
var url = stageUrl + '/a';
var xhr = createCORSRequest(method, url);
xhr.withCredentials = false;
xhr.onload = function() {
document.getElementById("result").innerHTML = "SUCCESS";
};
xhr.onerror = function() {
document.getElementById("result").innerHTML = "ERROR";
};
xhr.send();
}
var createCORSRequest = function(method, url) {
var xhr = new XMLHttpRequest();
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>