简体   繁体   English

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

[英]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 : 并且我正在使用此javascript发送POST请求:

$.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 !!! 我收到415-不支持的媒体类型错误!!! any idea ? 任何想法 ?

You want to set contentType . 您要设置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 适当的标头需要在其中包含'Content-Type' ,jQuery会为您处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM