title: Add Access-Control-Allow-Origin for CROS
body: <p>I'm developping a spring mvc rest api. I added <code>Access-Control-Allow-Origin</code> in the response.</p>
  
  <p>With PostMan, I found the header like this :</p>
  
  <pre><code>Access-Control-Allow-Methods →POST, GET, OPTIONS, DELETE
  Access-Control-Allow-Origin →*
  Content-Type →application/json;charset=UTF-8
  Server →Apache-Coyote/1.1
  Transfer-Encoding →chunked
  </code></pre>
  
  <p>But when I test it with a <em>test.html</em>, it shows error :</p>
  
  <pre><code>XMLHttpRequest cannot load http://localhost:8080/rest/v1/tokens. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 
  </code></pre>
  
  <p>I don't know why because by Postman, <code>Access-Control-Allow-Origin</code> is already there. Maybe it's the problem of <code>application/json</code>?</p>
  
  <p>Ajax part :</p>
  
  <pre><code>function tokens_Post(username,password){
  
      $.ajax({
          type:"POST",
          url:"http://localhost:8080/rest/v1/tokens",
          dataType: "json",
          contentType : "application/json",
          data:{username:username,password:password},
          success:show_user_info,
          error:checkError
      });
  
  }
  </code></pre>
  
  <p>Server code :</p>
  
  <pre><code>@RequestMapping(value = RestURIConstants.TOKEN_POST, method = RequestMethod.POST)
  public @ResponseBody ResponseEntity&lt;Token&gt; TokenPOST(
          @RequestBody User user, HttpServletRequest rq, HttpServletResponse rp) {
  
      RestTemplate restTemplate = new RestTemplate();
  
      ResponseEntity&lt;Token&gt; response = 
              restTemplate.postForEntity(Token_post_url, user, Token.class);
  
      rp.addHeader("Access-Control-Allow-Origin", "*");
      rp.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
  
      response = new ResponseEntity&lt;Token&gt;(response.getBody(), HttpStatus.OK);
      System.out.println(response.getBody().toString());
  
      return response;
  
  }
  </code></pre>
  
  <p>The server code, it uses another rest api.</p>
  
  <p>Converter </p>
  
  <pre><code>&lt;!-- Configure to plugin JSON as request and response in method handler --&gt;
  &lt;beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"&gt;
      &lt;beans:property name="messageConverters"&gt;
          &lt;beans:list&gt;
              &lt;beans:ref bean="jsonMessageConverter"/&gt;
          &lt;/beans:list&gt;
      &lt;/beans:property&gt;
  &lt;/beans:bean&gt;
  
  &lt;!-- Configure bean to convert JSON to POJO and vice versa --&gt;
  &lt;beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"&gt;
  &lt;/beans:bean&gt;   
  </code></pre>