Spring RestTemplate call to API worked but jQuery failed because same-origin policy
<p>When I use Spring RestTemplate to call a Rest API.</p>

<pre><code>public class JiraBusImpl implements JiraBus {
  private RestTemplate restTemplate = new RestTemplate();

  HttpHeaders headers = BusUtils.createHttpHeaderWithDefaultBasicAuth();

  @Override
  public List&lt;JIRAProject&gt; getProjects() {
    HttpEntity&lt;String&gt; request = new HttpEntity&lt;String&gt;(headers);
    ResponseEntity&lt;JIRAProject[]&gt; response = restTemplate.exchange("http://jira_url:port/rest/api/2/project",
            HttpMethod.GET, request, JIRAProject[].class);
    JIRAProject[] projectsField = response.getBody();

    return Arrays.asList(projectsField);
  }
}
</code></pre>

<p>It worked normally, but when I use jQuery to call, it failed and throws an error</p>

<blockquote>
  <p>XMLHttpRequest cannot load url. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<a href="http://localhost:8080" rel="nofollow">http://localhost:8080</a>' is therefore not allowed access.</p>
</blockquote>

<p>Here is my jQuery code: </p>

<pre><code>$.ajax({
  url: 'http://jira_url:port/rest/api/2/project',
  type: 'GET',
  dataType: 'json',
  beforeSend: function (xhr) {
    xhr.setRequestHeader ("Authorization", "Basic " + btoa('username' + ":" + 'password'));
  },
  success: function(data) {
    console.log(data);
  },
  error: function(jqXHR, textStatus, errorThrown) {
  }
});
</code></pre>

<p>How can RestTemplate do that?</p>