title: How to make http authentication in REST API call from javascript
body: <p>I need to call <a href="https://wiki.openmrs.org/display/docs/API" rel="nofollow noreferrer">OpenMRS REST API</a> from Java script to get data from OpenMRS. Below is my java script code:</p>
  
  <pre><code>    function myfunction(){
  
      var xhr = new XMLHttpRequest();
  
      xhr.open("GET", "http://localhost:8081/openmrs-standalone/ws/rest/v1/person?q=John", false);
      xhr.setRequestHeader("Authorization: Basic YWRtaW46QWRtaW4xMjM");
  
      xhr.send("");
      alert(xhr.status);
  
      }
  </code></pre>
  
  <p>Where <code>YWRtaW46QWRtaW4xMjM</code> is my base64 coded username:password as explained <a href="https://stackoverflow.com/questions/15006705/how-to-use-basic-authentication-with-jira-rest-api-in-javascript">here</a>. If I do not put the authorization line in the code and check the web app using Firebug, it returns 401 unauthorized status that is expected. But if I put the authorization, nothing is returned and in firebug I do not see any response as well. If I check the URL directly on browser, the page asks for username and password and after giving correct credential, it returns the data normaly. So I am getting some problem of providing the http authentication right from the java script of the app. I have also considered the methods explained <a href="https://stackoverflow.com/questions/491914/pure-javascript-code-for-http-basic-authentication">here</a> but no luck. Can anyone please help me to authorize the http request right from the javascript?</p>