开发者

passing request params from jQuery to jersey service using json

开发者 https://www.devze.com 2023-01-03 08:11 出处:网络
im trying to POST (cross domain) some data to a jersey web service and retrieve a response (a GenericEntity object). The post successfully gets mapped to my jersey endpoint however when i pull the par

im trying to POST (cross domain) some data to a jersey web service and retrieve a response (a GenericEntity object). The post successfully gets mapped to my jersey endpoint however when i pull the parameters from the request they are empty..

$ .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');
     }
  });

this is my jersey endpoint..

@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 ar开发者_如何学运维gLastName)

is there something missing from my $.ajax call?


Try this:

$ .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');
     }
  });


you cannot use @QueryParam to obtain the values. U can use an object to fetch the values. Like so

@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  
}  
0

精彩评论

暂无评论...
验证码 换一张
取 消