taboubim taboubim - 4 months ago 12
Ajax Question

POST request returns 415 - Unsupported Media Type

Even if this error is known, I was not able to solve my issue!

The reset Service is declared in this code :

@POST
@Transactional
@Consumes(MediaType.APPLICATION_JSON)
@Path("/addProduct")
public void addProductToShoppingBag(JSONObject object) throws JSONException


and I'm sending a POST request using this javascript :

$.ajax({
header: 'application/json',
type: 'POST',
data: $.toJSON({
member_id: "1",
products_id: ["0","1"]
}),
url: url
}).done(success).error(failure);


and I'm getting the 415 - Unsupported Media Type error !!! any idea ?

Answer

You want to set contentType. Your header is not formatted properly

Switch:

 header: 'application/json',

To

contentType: "application/json;charset=utf-8",

The proper header needs to include 'Content-Type' in it and jQuery will take care of that for you