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

POST request returns 415 - Unsupported Media Type

本文关键字:媒体 类型 不支持 415- 请求 返回 POST      更新时间:2023-09-26

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

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

 @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: 'application/json',

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

正确的标头需要包含'Content-Type',jQuery将为您处理