Trying to POST as a json to a RESTful web service using jquery ajax and jersey
===

I have made a RESTful web-service.
    
    @POST
    @Path("/test")
    @Consumes({ MediaType.APPLICATION_JSON})
    public String test(TestObject to)
    {
        System.out.println(to.getTestString());
        return "SUCCESS";
    }

I have the object created with the @XmlRootElement
  
    @XmlRootElement
    public class TestObject implements Serializable {
        private static final long serialVersionUID = 1L;
   
        private String testString;
   
        public TestObject() {}
   
        public TestObject(String testString) {
            this.testString = testString;
        }
   
        public String getTestString() {
            return testString;
        }
        public void setTestString(String testString) {
            this.testString = testString;
        }
    }

I then try to call it with the following ajax call

    $.ajax({
        url: 'http://localhost:8080/testPage/test/',
        type: 'POST',
        data: '{"testString":"test"}',
        dataType: 'text',
        contentType: "application/json; charset=utf-8",
        success: function(jqXHR, textStatus, errorThrown){
            alert('Success');
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert("jqXHR - " + jqXHR.statusText + "\n" + 
                  "textStatus - " + textStatus + "\n" + 
                  "errorThrown - " + errorThrown);
        }
    });

I end up getting simply 'error' back for the textStatus.  It doesn't seem to be even reaching my test service.  I am to get GET to work and even POST to work when I only pass a text/plain.  When I try to pass a json I can't get it to work.  

Using the [POSTER](https://addons.mozilla.org/en-US/firefox/addon/poster/)! add-on for Firefox I am able to successfully call the service passing in the same data.  I added in some extra logging to catch the request headers on the service side so it does seem to see the request at least, but it does nothing with it.

Below are the requests that I got from the logs.  Top one being the failed one with ajax.  The bottom one being the one that succeeded with POSTER. (not really code but I don't see anything better to put it in)

    INFO: 1 * Server in-bound request
    1 > OPTIONS http://localhost:8080/testPage/test 
    1 > Host: localhost:8080
    1 > User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1
    1 > Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    1 > Accept-Language: en-us,en;q=0.5
    1 > Accept-Encoding: gzip, deflate
    1 > DNT: 1
    1 > Connection: keep-alive
    1 > Origin: http://localhost:8081
    1 > Access-Control-Request-Method: POST
    1 > Access-Control-Request-Headers: content-type
    1 > Pragma: no-cache
    1 > Cache-Control: no-cache
    1 > 

    INFO: 2 * Server in-bound request
    2 > POST http://localhost:8080/testPage/test
    2 > Host: localhost:8080
    2 > User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1
    2 > Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    2 > Accept-Language: en-us,en;q=0.5
    2 > Accept-Encoding: gzip, deflate
    2 > DNT: 1
    2 > Connection: keep-alive
    2 > Content-Type: application/json; charset=utf-8
    2 > Content-Length: 22
    2 > Cookie: JSESSIONID=bvizai6k0277
    2 > Pragma: no-cache
    2 > Cache-Control: no-cache
    2 >
    {"testString": "test"}

From this is seems that is is not getting the json being passed to the service.  I have tried as above writing out the json and I have tried using JSON.stringify to create it with neither succeeding.

Does anyone know what I am doing wrong when trying to send a json to a RESTful web-service using POST in an jquery ajax call?