0

I am using jersey on the server side and my function uses

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response someFunction(SomeClass obj,@Context HttpServletRequest req){}

But when i make a http post call with content-type="application/json" and payload as {"name":"abc","age":"20"} from Advanced Rest Client, then the call will be a successfull call. But if i create a client which makes an ajax call like,

 var person = {
            name: "abc",
            age:"20",
        }
 $.ajax({
    url: 'someUrl',
    type: 'post',
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    success: function (data) {
    },
    data: person
    }); 

Then this call will return 400 bad request and it is not going inside the function at server side. If we pass person object in the form of a string like,

"{\"name\":\"abc\",\"\age":\"20\"}"

Then ajax call also return 200. Any reason why it is happening?

sunder
  • 968
  • 2
  • 11
  • 31

1 Answers1

0

You should stringify data.

$.ajax({
   url: 'someUrl',
   type: 'post',
   contentType: "application/json; charset=utf-8",
   dataType: 'json',
   success: function (data) {
   },
   data: JSON.stringify(person)
}); 
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60