0

I tried to do HttpRequest by JavaScript. The server needs :

  • basic Authorization
  • 'Accept' : 'application/json'
  • 'Content-Type' : 'application/json'
  • 'Content-Length' : '*'
  • body written by JSON

I received the following error message.

XMLHttpRequest cannot load -my url-. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 404.

I received the above error message, even though I have already run it through python's SimpleHTTPServer.

Here's my code.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script language="javascript" type="text/javascript">
     function OnButtonClick() {

  var clientId = "-my ID-";
  var clientSecret = "-my password";

  var authorizationBasic = window.btoa(clientId + ':' + clientSecret);

  var request = new XMLHttpRequest();

  request.open('POST', "-my url-");
  request.setRequestHeader('Authorization', 'Basic ' + authorizationBasic);
  request.setRequestHeader('Accept', 'application/json');
  request.setRequestHeader('Content-Type', 'application/json');
  request.send(JSON.stringify({email: "-my email-" }));

    request.onreadystatechange = function () {
        if (request.readyState === 4) {
           alert(request.responseText);
        }
      };
 }
</script>
</head>
<body>
  <input id="Button1" type="button" value="送信" onclick="OnButtonClick();" />
  <div id="output"></div>
</body>
</html>

Is there anything wrong with javascript?

0 Answers0