• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<body bgcolor="white">
3<script language="JavaScript">
4function execXMLHttpRequest()
5{
6  var url = document.getElementById("url").value;
7  var warningElement = document.getElementById("warning");
8  if (url.indexOf(location.origin) != 0) {
9    warningElement.innerHTML =
10      'For cross-origin requests to succeed the server must return CORS headers:' +
11      '<pre>Access-Control-Allow-Origin: ' + location.origin +
12      '<br/>Access-Control-Allow-Header: My-Custom-Header</pre>';
13    warningElement.style.display = 'block';
14  } else {
15    warningElement.style.display = 'none';
16  }
17
18  xhr = new XMLHttpRequest();
19  xhr.open("GET", url, true);
20  xhr.setRequestHeader('My-Custom-Header', 'Some Value');
21  xhr.onload = function(e) {
22    if (xhr.readyState === 4) {
23      var value = "Status Code: "+xhr.status;
24      if (xhr.status === 200)
25        value += "\n\n"+xhr.responseText;
26      document.getElementById('ta').value = value;
27    }
28  }
29  xhr.send();
30}
31</script>
32<form>
33URL: <input type="text" id="url" value="http://tests/request">
34<br/><input type="button" onclick="execXMLHttpRequest();" value="Execute XMLHttpRequest">
35<br/><textarea rows="10" cols="40" id="ta"></textarea>
36</form>
37<div id="warning" style="display:none;font-weight:bold;"></div>
38</body>
39</html>
40