POST 请求 returns 415 - 不支持的媒体类型

POST request returns 415 - Unsupported Media Type

即使知道这个错误,我也无法解决我的问题!

重置服务在此代码中声明:

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

我正在使用 javascript 发送一个 POST 请求:

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

我收到 415 - 不支持的媒体类型错误!!!有什么想法吗?

您想设置contentType。您的 header 格式不正确

切换:

 header: 'application/json',

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

正确的 header 需要在其中包含 'Content-Type',jQuery 会为您处理