title: Spring MVC Ajax POST
body: <p>I'm trying to post a JSON Object, with Ajax, to an Rest Spring MVC Controller, but I met some troubles.</p>
  
  <p>Let's present you my code:</p>
  
  <p>Controller</p>
  
  <pre><code>@RequestMapping(value = "/register/checkUsername.html", method = RequestMethod.POST,  produces=MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody JsonResponse checkUsername(@RequestBody JsonUsername username) {
      String usernameString = username.getUsername();
      JsonResponse response = new JsonResponse();
      response.setMessage(usernameString + "Available");
      return response;
  }
  </code></pre>
  
  <p>Ajax Function</p>
  
  <pre><code>   function displayUsernamError(data) {
      var json = "&lt;h4&gt;Eroare&lt;/h4&gt;&lt;pre&gt;"
              + JSON.stringify(data, null, 4) + "&lt;/pre&gt;";
      $('#usernameError').html(json);
  }
  
  function checkUsername(){
      var username = document.getElementById("username");
      var search = {
              "username" : username.value
      }
  
      $.ajax({
              type : "POST",
              contentType : 'application/json; charset=utf-8',
              dataType : 'json',
              url : "http://localhost:8080/skill-assessment/register/checkUsername.html",
              data : JSON.stringify(search),
              success : function(result) {
                  console.log("SUCCESS: ", data);
                  displayUsernamError(result);
              },
              error: function(e){
                  console.log("ERROR: ", e);
                  displayUsernamError(e);
              },
              done : function(e) {
                  console.log("DONE");
              }
      });
  }
  </code></pre>
  
  <p>And finally the html form:</p>
  
  <pre><code>&lt;form:form modelAttribute="user" method="POST" enctype="utf8"&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;label&gt;Username&lt;/label&gt;&lt;/td&gt;
              &lt;td&gt;&lt;form:input path="name" id="username" /&gt;&lt;/td&gt;
              &lt;td&gt;
              &lt;p id="usernameError"&gt;
              &lt;/p&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;button type="button" onClick="checkUsername()"&gt;Register&lt;/button&gt;
      &lt;/form:form&gt;
  </code></pre>
  
  <p>So, I'm calling the checkUsername method when I press click on the Register button, only for test. </p>
  
  <p>The ajax function is called and this send the JSON to the controller. I used the debug and the controller seems to get the JSON Object.</p>
  
  <p>The problem is with the controller Response, because in the error div from my html page, I get this error:</p>
  
  <p>HTTP Status 406
  <em>The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request \"accept\" headers.</em></p>
  
  <p>Where is the problem? Who can give me an idea?</p>