我有一个 JAX-RS 项目,其中 POST
不起作用。我有 @GET
URL,工作正常。除了这个 @POST
之外,一切似乎都工作正常。
@POST
@Path("/json/insert")
@Produces(MediaType.APPLICATION_JSON)
@Consumes("application/x-www-form-urlencoded")
public String postJSONInsert(
@FormParam("instance") String instance,
@FormParam("db") String table) {
String json;
EDPObject edp_obj = new EDPObject();
try {
json = edp_obj.insert("json", instance, table);
} catch(Exception e) {
edp_obj.endSession();
json = handleJSONError(e);
}
return json;
}
在客户端尝试此操作时,在 Firebug 中获取 500 尚未连接
:
$.ajax('http://127.0.0.1:8070/sixaxis/webapi/json/insert', {
data: {
db: '17:2',
instance: 'shawn'
},
dataType: 'json',
type: 'POST'
});
最佳答案
你尝试过吗:
@Consumes(MediaType.APPLICATION_JSON)
因为你的 jQuery 是:
dataType:'json'
更新(感谢反馈): 那么方法至少应该是:
@POST
@Path("/json/insert")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String postJSONInsert( Map<String,Object> params ){
// Your business logic
}
关于java - 使用 JSON 作为 application/x-www-form-urlencoded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19645577/