我尝试将一些数据POST(跨域)到泽西网络服务并检索响应(GenericEntity对象)。帖子成功地映射到我的球衣端点但是当我从请求中提取参数时它们是空的..
$ .ajax({
type: "POST",
dataType: "application/json; charset=utf-8",
url: jerseyNewUserUrl+'?jsoncallback=?',
data:{'id':id, 'firstname':firstname,'lastname':lastname},
success: function(data, textStatus) {
$('#jsonResult').html("some data: " + data.responseMsg);
},
error: function ( XMLHttpRequest, textStatus, errorThrown){
alert('error');
}
});
这是我的球衣端点..
@POST
@Produces( { "application/x-javascript", MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/new")
public JSONWithPadding addNewUser(@QueryParam("jsoncallback")
@DefaultValue("empty")
final String argJsonCallback, @QueryParam("id")
final String argID, @QueryParam("firstname")
final String argFirstName, @QueryParam("lastname")
final String argLastName)
我的$ .ajax电话中缺少什么?
答案 0 :(得分:1)
试试这个:
$ .ajax({
type: "POST",
dataType: "jsonp",
jsonp: "fooCallBackFunction",
url: jerseyNewUserUrl,
data:{'id':id, 'firstname':firstname,'lastname':lastname},
success: function(data, textStatus) {
$('#jsonResult').html("some data: " + data.responseMsg);
},
error: function ( XMLHttpRequest, textStatus, errorThrown){
alert('error');
}
});
答案 1 :(得分:1)
您不能使用@QueryParam来获取值。你可以使用一个对象来获取值。像这样
@POST
@Produces( { "application/x-javascript", MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@Path("/new")
public void addNewUser(User user){
//NB User has to have the following field names id', firstname,lastname
}