title: Jersey/Jquery HTTP communication and JSON data
body: <p>I have some trouble making Jersey and Jquery communicate data in JSON format.
  In the following minimal example always a HTTP 204 is returned to the calling JQuery client.</p>
  
  <pre><code>@Path("/path")
  @Produces(MediaType.APPLICATION_JSON)
  @Consumes(MediaType.APPLICATION_JSON)
  public class Resource {
      @POST
      public ReturnData postSomething(@Valid Data data) {
        Log("success");
        ...
        ReturnData returnData = ...;
        return returnData;
      }
  }
  </code></pre>
  
  <p>I use the following JQuery code and the error function is called after each post.</p>
  
  <pre><code>$.ajax({
       type: "POST",
       contentType: "application/json; charset=utf-8",
       url: url,
       data: '{"data": "data"}',
       dataType: "json",
       error: function(data){
            alert("fail");
       },
       success: function(data){
            alert("success");
       }
       });
  </code></pre>
  
  <p>In Firebug I see the following HTTP Request/Response with the setup described above:</p>
  
  <pre><code>OPTIONS /path HTTP/1.1
  Host: 192.168.15.109:8080
  User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Accept-Language: en-us,en;q=0.5
  Accept-Encoding: gzip, deflate
  Connection: keep-alive
  Origin: http://localhost
  Access-Control-Request-Method: POST
  Access-Control-Request-Headers: content-type
  Pragma: no-cache
  Cache-Control: no-cache
  
  HTTP/1.1 204 No Content
  Date: Thu, 24 May 2012 16:10:57 GMT
  Allow: OPTIONS,POST
  </code></pre>
  
  <p>At first I had tried a setup without contentType: "application/json; charset=utf-8" in the JQuery post which turned out as follows:</p>
  
  <pre><code>POST /path HTTP/1.1
  Host: 192.168.15.109:8080
  User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0
  Accept: application/json, text/javascript, */*; q=0.01
  Accept-Language: en-us,en;q=0.5
  Accept-Encoding: gzip, deflate
  Connection: keep-alive
  Content-Type: application/x-www-form-urlencoded; charset=UTF-8
  Referer: http://localhost/test.html
  Content-Length: 104
  Origin: http://localhost
  Pragma: no-cache
  Cache-Control: no-cache
  
  HTTP/1.1 415 Unsupported Media Type
  Date: Thu, 24 May 2012 16:08:55 GMT
  Content-Type: text/plain;charset=UTF-8
  Cache-Control: must-revalidate,no-cache,no-store
  Content-Length: 72
  Cache-Control: no-cache
  </code></pre>
  
  <p>I'm thinking that client and server can't find a datatype they both agree on for some reason. I find it strange that the accept header doesnt include JSON when setting the contentType to be JSON.</p>
  
  <p>The Log("success"); on the server side is never executed.</p>
  
  <p>Can anybody point me into the right direction?</p>