简体   繁体   中英

Ajax Post request to a Jax-rs service

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 Type is 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' 
},

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

Default Content-Type of jQuery.ajax() call is application/x-www-form-urlencoded; charset=UTF-8 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM