title: Calling REST endpoint from Javascript
body: <p>I am trying to call Spotify API from Javascript like this:</p>
  
  <pre><code>function callAPI() {
      var xhttp = new XMLHttpRequest();
      xhttp.open('GET', 'https://api.spotify.com/v1/search?q=Muse&amp;type=track');
      xhttp.setRequestHeader('Content-Type', 'application/json');
      xhttp.setRequestHeader('Authorization', 'Bearer &lt;MY_ACCESS_TOKEN&gt;');
      xhttp.send();
      var response = JSON.parse(xhttp.responseText);
  }
  </code></pre>
  
  <p>However, <code>xhttp.responseText</code> is empty, although the request returns 200.</p>
  
  <p>I can see in the browser that the request is returning the expected result but somehow I cannot extract it from the Javascript:
  <a href="https://i.stack.imgur.com/jIS20.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jIS20.png" alt="enter image description here"></a></p>
  
  <p>I tried calling the REST endpoint using Java (since I am more familiar with it) and that worked:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.URL;
  
  public class Test {
      public static void main(String[] args) {
          System.out.println("blah");
  
          URL url = null;
          try {
              url = new URL("https://api.spotify.com/v1/search?q=Muse&amp;type=track");
              HttpURLConnection connection = (HttpURLConnection) url.openConnection();
              connection.setRequestMethod("GET");
              connection.setRequestProperty("Authorization", "Bearer &lt;MY_ACCESS_TOKEN&gt;");
              String responseMessage = connection.getResponseMessage();
              BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
              StringBuffer content = new StringBuffer();
  
              try (reader) {
                  String line;
  
                  while ((line = reader.readLine()) != null) {
                      content.append(line);
                  }
              } finally {
                  reader.close();
              }
              System.out.println("Response: " + responseMessage);
              System.out.println("Content: " + content.toString());
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>}</p>
  
  <p>I am a Javascript newbie, so what am I doing wrong? Thanks a lot in advance!</p>