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

时间:2016-07-19 05:03:37

标签: javascript ajax web-services jersey http-status-code-415

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

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

 @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 - 不支持的媒体类型错误!任何想法?

1 个答案:

答案 0 :(得分:6)

您想要设置 **1 JsonSchema schema = new JsonSchema(); 2 schema.Type = JsonSchemaType.Object; 3 schema.Properties = new Dictionary<string, JsonSchema> 4{ 5 { "name", new JsonSchema { Type = JsonSchemaType.String } }, 6 { 7 "hobbies", new JsonSchema 8 { 9 Type = JsonSchemaType.Array, 10 Items = new List<JsonSchema> { new JsonSchema { Type = JsonSchemaType.String } } 11 } 12 }, 13};** 。您的标题格式不正确

开关:

contentType

 header: 'application/json',

正确的标头需要包含contentType: "application/json;charset=utf-8", ,jQuery会为你处理