title: Ajax request is not calling Spring boot controller
body: <p>I'm trying to call my Spring controller using Ajax and submitting a form. </p>
  
  <p>Function always retrieves the error window. I tried changing the URL parameter to <strong>"/profile"</strong>, <strong>"profile"</strong> or <strong>"PrivateAreaController/profile"</strong>, but I keep getting the same error.</p>
  
  <p>My <code>main.js</code> file and controller are placed in the following order:</p>
  
  <pre><code>--&gt;Mainfolder
      --&gt;src
         --&gt;java
            --&gt;controller
                --&gt;PrivateAreaController.java
         --&gt;resources
             --&gt;static
                --&gt;js
                   --&gt;main.js
  </code></pre>
  
  <p>My controller is called <code>PrivateAreaController</code></p>
  
  <p><strong>Ajax Code</strong></p>
  
  <pre><code>$('#sampleForm').submit(
      function(event) {
          var firstname = $('#firstname').val();
          var lastname = $('#lastname').val();
          var data = 'firstname='
              + encodeURIComponent(firstname)
              + '&amp;amp;lastname='
              + encodeURIComponent(lastname);
          $.ajax({
              type : "POST",
              dataType: "json",
              url : '@Url.Action("callingcontroller","PrivateAreaController")',
              contentType: "application/json; charset=utf-8",
              data : data,
              success : function(response) {
                  alert( response );
              },
              error : function() {
                  alert("not working");
              }
          });
          return false;
      }); 
  </code></pre>
  
  <p><strong>Spring code</strong></p>
  
  <pre><code>@RequestMapping(value = "/profile", method = RequestMethod.POST)
          public @ResponseBody
          String processAJAXRequest(
                  @RequestParam("firstname") String firstname,
                  @RequestParam("lastname") String lastname   ) {
              String response = "";
  
              System.out.println("working");
              return response;
          }
  </code></pre>
  
  <p><strong>HTML form</strong></p>
  
  <pre><code>&lt;form id="sampleForm" method="post" action="/profile"&gt;
           &lt;input type="text" name="firstname" id="firstname"/&gt;
           &lt;input type="text" name="lastname" id="lastname"/&gt;
           &lt;button type="submit" name="submit"&gt;Submit&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I found the answer.. i needed to add     </p>
  
  <pre><code>@CrossOrigin(origins = "http://localhost:8080")
  </code></pre>
  
  <p>before the <code>@RequesMapping</code> parameter and change the url parameter of the ajax call to url: '<a href="http://localhost:8080/(your" rel="nofollow noreferrer">http://localhost:8080/(your</a> requestmapping parameter)</p>