title: Ajax POST results in a 405 (Method Not Allowed) - Spring MVC
body: <p>I'm trying to make an ajax call to my Spring controller/action with POST method, and return an object from the server with @ResponseBody. The strange situation is that it stop working after add spring security layer, everything was working fine before. I will try to explain my moves to solve the problem and then show you the code/captures/etc.</p>
  
  <p>1.
  After some research I found some answers telling the problem might be related with csrf mechanism, so I disabled it and still have the issue. (spring-security.xml bellow)</p>
  
  <p>2.
  I've made a wireshark capture to check the request/response. My ajax request is OK, my controller declaration is OK, but I don't understand why, the 405 response indicates > <strong>Allow: GET</strong> (capture bellow)</p>
  
  <p>3.
  I've tried to access my controller action through the browser (i.e., make an GET request), and I get the error <strong>HTTP Status 405 - Request method 'GET' not supported</strong>!</p>
  
  <p>4.
  I've tried to change the RequestMapping(method...) to RequestMethod.GET and the request arrives to the controller and works fine, but I don't want it to work on GET method, I want a POST request.</p>
  
  <p>5.
  Changed the RequestMapping(consumes, produces, headers) to accept all kind of data, but still 405...</p>
  
  <p>This is driving me crazy! I post my files bellow, so you can check it guys, any tip will be appreciated. Thanks! (IMPORTANT NOTE: this is my despair configuration)</p>
  
  <p><em>spring-security.xml</em></p>
  
  <pre><code>&lt;beans:beans 
       xmlns...(all needed declarations)&gt;
  
  &lt;http pattern="/js/**" security="none" /&gt;
  &lt;http pattern="/css/**" security="none" /&gt;
  
  &lt;!-- enable use-expressions --&gt;
  &lt;http auto-config="true" &gt;
      &lt;access-denied-handler error-page="/403" /&gt;
      &lt;intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" /&gt;
      &lt;intercept-url pattern="/login" access="isAnonymous()" /&gt;
      &lt;intercept-url pattern="/403" access="permitAll" /&gt;
      &lt;intercept-url pattern="/**" access="hasRole('ROLE_USER')" /&gt;
  
      &lt;form-login  login-page="/login"
                   username-parameter="email"
                   password-parameter="password"
                   authentication-failure-url="/login?failed" /&gt;
  
      &lt;!--
      &lt;csrf/&gt;
      --&gt;
  &lt;/http&gt;
  
   ..... (authentication)  
  </code></pre>
  
  <p><em>AdminController.java</em></p>
  
  <pre><code>@Controller
  @RequestMapping("/admin**")
  public class AdminController {
  
      ... (all my autowired beans)
  
      @RequestMapping(
          value = "/events/loadEvents",
          method = RequestMethod.POST,
          consumes = MediaType.ALL_VALUE,
          produces = MediaType.ALL_VALUE,
          headers = "Accept=*/*")
      @ResponseBody
      public Event loadEvents(@RequestParam("parentId") long parentId) {
          ... (my logic)
          return event;
      }
  }
  </code></pre>
  
  <p>Request (wireshark capture)
  <a href="https://i.stack.imgur.com/TgsH8.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TgsH8.jpg" alt="HTTP Request (link smudged because I&#39;ve used a simplified on my question)"></a></p>
  
  <p>Response (wireshark capture)
  <a href="https://i.stack.imgur.com/au3Tw.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/au3Tw.jpg" alt="enter image description here"></a></p>
  
  <p><strong>EDIT</strong>
  <em>jquery ajax call code</em></p>
  
  <pre><code>$.ajax({
      type: 'POST',
      cache: false,
      url: /admin/events/loadEvents,
      data: { parentId: 1 },
      dataType = 'json',
      contentType = 'application/json',
  
      ...
  });
  </code></pre>