0

I'm trying to do a simple post request from a browser and 415 Unsupported Media is show in browser's console, in the console it says too that the Typeis text/html, maybe I'm mising something stupid here but seriously I'm doing post request from an android client and everything is find with the server side, so i guess (since I'm not familiar with js) it's a javascript problem I'm having here, here are the portions of interest of the code:

ajax (this function is called send and it does some things before to create the json, that part it's ok and tested, the json is being genereated successfully):

$.ajax({
        url: 'webresources/serverConfig/save/',
        type: 'post',
        dataType:'json',
        data: jsonObj
    });

how i call the javascript in the HTML form:

<form action="javascript:send()"  method="post">

JAX-rs service:

@Path("serverConfig/")
public class ConfigurationSaverService {

@POST
    @Path("save/")
    @Consumes(MediaType.APPLICATION_JSON)
    public void save(Configuration configuration){
    //config stuffs here.
}

Edit: due to @adrianplattner's answer, it's important to say that I'm using glassfish 4.0, so i didn't need to add jersey's dependencies, I also try glashfish 3.1 and still get the same HTTP error.

EDIT 2: Header:

 headers: { 
    'Accept': 'application/json',
    'Content-Type': 'application/json' 
},
jac1013
  • 408
  • 3
  • 11

2 Answers2

0

you can add jersey dependency:

 <dependency>
     <groupId>com.sun.jersey</groupId>
     <artifactId>jersey-json</artifactId>
     <version>1.11</version>
 </dependency>

that should resolve ur problem

0

Default Content-Type of jQuery.ajax() call is application/x-www-form-urlencoded; charset=UTF-8 (see documentation) but you're expecting to consume application/json in your JAX-RS resource. Set the contentType parameter of your call to application/json.

Michal Gajdos
  • 10,339
  • 2
  • 43
  • 42
  • i did this but it didn't work, I also try to set some parameters in header like theon's answer in here http://stackoverflow.com/questions/11492325/post-json-fails-with-415-unsupported-media-type-spring-3-mvc but it didn´t solve the issue, do i need a Entity wrapper or something? – jac1013 Aug 07 '13 at 19:47
  • Can you try to turn on LoggingFilter (as described in `EDIT 1` in this answer http://stackoverflow.com/a/18033725/290799) and share the headers of the request with us? – Michal Gajdos Aug 07 '13 at 20:42
  • edited with header now, and i don't know how to make my maven Jax-rs project to produce the web.xml and i don't really want to add it manually. – jac1013 Aug 07 '13 at 21:05
  • You don't need to create `web.xml`, there are 3 ways how to register the LoggingFilter (all of them are described in the link). I was curious about headers that come to the server and are logged into the server log (when LoggingFilter is enabled). – Michal Gajdos Aug 08 '13 at 13:43
  • i added "@XmlRootElement" to Configuration class and it gave me a Bad Request error, without the XmlRoo...t Anotation i did a successful post request from an Android client. – jac1013 Aug 08 '13 at 16:49