使用 jquery 调用 JAX-RS



我正在使用JAX-RS来构建Restufl Web应用程序,使用json对象的post方法调用Web服务时遇到问题。我必须使用 jquery
调用 Web 服务这是方法

 @POST
@Path("/client")
@Consumes(MediaType.APPLICATION_JSON) 
public Response showClient(Client c){
     String name = c.getAdresseCl() + ""+ c.getEmailCl();
    ResponseBuilder builder = Response.ok(name);
    builder.header("Access-Control-Allow-Origin", "*");
    builder.header("Access-Control-Max-Age", "3600");
    builder.header("Access-Control-Allow-Methods", "GET");
    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();
}

使用 jquery 进行客户端调用:

  var data={'adressCl':'myAdress','emailCl':'example@domain.com'};
$.ajax({
"type":"post",
"dataType":"json",
"data":data,
"url":"http://localhost:9080/FournisseurWeb/jaxrs/clients/client",
"success":function(res){
console.log(res);
}
});/*
$.post("http://localhost:9080/FournisseurWeb/jaxrs/clients/client",data,function(res){
console.log(res);
});*/

我收到此错误:"网络错误:415 不支持的媒体类型请帮忙!提前感谢

从 http://api.jquery.com/jQuery.ajax/contentType字段描述来看,.ajax(( 默认发送的是 application/x-www-form-urlencoded,而不是 json,所以你需要正确设置它:application/json

我从来没有检查过,但我总是使用"type":"POST"而不是"type":"post",也许这就是问题所在。

最新更新