I’m trying to call a Webservice that consumes a json object with post method .I did it then It wont work again don’t know what is the problem.
here is my method
@POST
@Path("/post")
@Consumes("application/json")
@Produces("application/json")
public Response testClient(Client c) throws IOException {
System.out.println(c.getAdresseCl());
ResponseBuilder builder = Response.ok(c.getAdresseCl());
builder.header("Access-Control-Allow-Origin", "*");
builder.header("Access-Control-Max-Age", "3600");
builder.header("Access-Control-Allow-Methods", "*");
builder.header(
"Access-Control-Allow-Headers",
"X-Requested-With,Host,User-Agent,Accept,Accept-Language,Accept-Encoding,Accept-Charset,Keep-Alive,Connection,Referer,Origin");
return builder.build();
}
to call this I used this
$.ajax({
type: 'POST',
url: "http://localhost:9080/FournisseurWeb/jaxrs/clients/post",
data: '{"adresseCl":"tunis"}',
dataType:'json',
contentType: "application/json; charset=utf-8",
success: function (msg) {
alert(msg);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('error');
}
});
well I remark that when I set the contentType to application/json the method changes to OPTIONS .
and when I don’t use the content type I got “415 Unsupported Media Type ” I dont know how to fix this. I passed too much time without results 🙁
thank you for helping me
this is the method that consumes a text xml fomat and map it to an object to persist it next
I use to call this method using ajax with this sample :
this is a jax-rs call with ajax POST using cross domain so hope that it helps 🙂
NOTE: The cross-domain call without JSONP is legal here because the server is returning the following header, which enables cross-domain AJAX!
See Mozilla Developer Center page on Access-Control-Allow-Origin for more details.