title: jquery ajax rest call - Unsupported Media Type
body: <p>I'm having a simple jquery ajax call to a rest service. I am setting the contentType as "application/json" and the rest resource is configured to accept "<strong>MediaType.APPLICATION_JSON</strong>". This is a POST method.
  With this setup, I am getting "<strong>Unsupported Media Type</strong>" error.  </p>
  
  <p><strong>The header info shows</strong> 
  "Content-Type   application/json; charset=UTF-8" in the request header</p>
  
  <p><strong>Response shows</strong>: Status report: Unsupported Media Type
  The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (Unsupported Media Type).</p>
  
  <p>Please provide some pointers to resolve this issue.</p>
  
  <p>Here is the code snippet:</p>
  
  <p><strong>Rest Resource</strong></p>
  
  <pre><code>@POST
  @Produces({MediaType.APPLICATION_JSON,MediaType.TEXT_HTML})
  @Consumes({MediaType.APPLICATION_JSON,MediaType.TEXT_HTML})
  public Response addPerson(MyJSONObj myObj) {
      //...  
      // ...
      //...
  }
  </code></pre>
  
  <p><strong>jquery</strong></p>
  
  <pre><code>$(document).ready(function() { /* put your stuff here */
      $("#Button_save").click(function(){
      var firstName = $('firstName').val(); 
      var lastName = $('lastName').val(); 
      var person = {firstName: firstName, lastName: lastName}; 
      $.ajax({
  
          url:'http://localhost:8080/sampleApplication/resources/personRestService/',
          type: 'POST',
          data: person,
          Accept : "application/json",
          contentType: "application/json",
  
          success:function(res){
          alert("it works!");
          },
          error:function(res){
              alert("Bad thing happend! " + res.statusText);
          }
      });
      });
  }); 
  </code></pre>
  
  <p><strong>Headers as displayed in FF Firebug</strong></p>
  
  <p>Response Headers</p>
  
  <pre><code>Content-Length  1117
  Content-Type    text/html;charset=utf-8
  Date    Thu, 05 Apr 2012 09:44:45 GMT
  Server  Apache-Coyote/1.1
  </code></pre>
  
  <p>Request Headers</p>
  
  <pre><code>Accept  */*
  Accept-Encoding gzip, deflate
  Accept-Language en-us,en;q=0.5
  Connection  keep-alive
  Content-Length  97
  Content-Type    application/json; charset=UTF-8
  Host    localhost:8080
  Referer http://localhost:8080/sampleApplication/
  User-Agent  Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0
  X-Requested-With    XMLHttpRequest
  </code></pre>