【问题标题】:call JAX-RS using jquery使用 jquery 调用 JAX-RS
【发布时间】:2012-05-31 02:29:28
【问题描述】:

我正在使用 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);
});*/

我收到此错误:“NetworkError: 415 Unsupported Media Type 请帮忙!并提前谢谢

【问题讨论】:

标签: jquery json jax-rs


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-27
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 2023-01-27
    • 1970-01-01
    相关资源
    最近更新 更多