title: Cross origin blocked between Ajax and Spring Controller
body: <p>I want a Javascript function to send data to a Spring controller and get a response. However, due to the strict-origin-when-cross-origin Referrer policy, the request does not go through.</p>
  <p>Spring Controller :</p>
  <pre><code>@Controller
  public class EventController {
  
  @ResponseBody
  @RequestMapping(value = &quot;/event&quot;, method = RequestMethod.POST)
  public String handleAjax(@RequestParam Integer id, HttpServletRequest request, HttpServletResponse response) {
  
      response.setHeader(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;);
  
      return &quot;OK&quot;;
  
  }
  }
  </code></pre>
  <p>Javascript functions :</p>
  <pre><code>function getContextPath() {
  return window.location.pathname.substring(0, window.location.pathname.indexOf(&quot;/&quot;,2));
  }  
  
  function move(moveDir,velocity){
  $.ajax({
      type : &quot;POST&quot;,
      url : getContextPath()  + &quot;/event&quot;,
      success: function(data){
          console.log(data)
      }
  });
  }
  </code></pre>
  <p>I know that I have to allow cross-origin for these files. So far, the things I tried didn't work.
  List of what I tried :</p>
  <p>-&gt; Adding <code>@CrossOrigin(origins = &quot;http://localhost:8081&quot;, maxAge = 3600)</code> to the controller</p>
  <p>-&gt; Adding <code>response.setHeader(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;);</code> to the controller</p>
  <p>-&gt; Adding <code>crossorigin=&quot;anonymous&quot;</code> to the Javascript <code>&lt;script&gt;</code> tag</p>