HTTP Status 415 - Unsupported media type for AJAX call in JQUERY for Restful WS implemented using JERSEY

Hi, I am trying to publish json data for Restful WS implemented using Jersey. I am sending data via jquery-ajax. Why am I getting an unsupported HTTP Status-415 content type? Thank. Click here to see a screenshot of the firebug description
 //post method handler 
      @Path("/newentry")
        public class NewEntry {

            @POST
            @Consumes(MediaType.APPLICATION_JSON)
            public Response newEntry(String data) {
                    //doStuff
        }
    }
    // ajax call 
         $.ajax({
                 url: "http://localhost:8080/FirstRestWebService/rest/newentry",
                 type: "post",
                 data: formToJSON(),
                 dataType : "json",
                 success: function(data){
                alert("success");
                  },
               error:function(jqXHR, textStatus, errorThrown) {
                    alert("failure");
                  }   
            }); 

         function formToJSON() {
                return JSON.stringify({
                    "name": $("input#emp_name").val(),
                    ...
                    "username": $('input#username').val(),
                    "password": $('input#password').val()
                    }); 

Click here to view a screenshot of the firebug description. I was able to successfully test WS using the Jersey Client . What is wrong with the above AJAX call? Thank.

+5
source share
2 answers

In your AJAX call, you need to set your content type:

contentType: "application/json"
+17

JSON. pom.xml.

 <dependency>
    <groupId>com.owlike</groupId>
    <artifactId>genson</artifactId>
    <version>0.98</version>
</dependency>
+1

All Articles