jquery - Spring MVC 415 不支持的媒体类型

标签 jquery ajax spring spring-mvc

我正在使用 Spring 3.2 并尝试使用 ajax 发布请求来提交 json 对象数组。如果这是相关的,我转义了所有特殊字符。

我的 HTTP 状态为 415。

我的 Controller 是:

@RequestMapping(value = "/save-profile", method = RequestMethod.POST,consumes="application/json")
    public @ResponseBody String saveProfileJson(@RequestBody String[] profileCheckedValues){
        System.out.println(profileCheckedValues.length);
        return "success";
    }

jquery 是:

jQuery("#save").click(function () {
        var profileCheckedValues = [];
        jQuery.each(jQuery(".jsonCheck:checked"), function () {
            profileCheckedValues.push($(this).val());
        });
        if (profileCheckedValues.length != 0) {
            jQuery("body").addClass("loading");
            jQuery.ajax({
                type: "POST",
                contentType: "application/json",
                url: contextPath + "/sample/save-profile",
                data: "profileCheckedValues="+escape(profileCheckedValues),
                dataType: 'json',
                timeout: 600000,
                success: function (data) {
                    jQuery('body').removeClass("loading");
                },
                error: function (e) {
                    console.log("ERROR: ", e);
                    jQuery('body').removeClass("loading");
                }
            });
        }
    });

我发布的数组中的一个对象的示例是以下 json:

{
  "id": "534213341",
  "name": "Jack Lindamood",
  "first_name": "Jack",
  "last_name": "Lindamood",
  "link": "https://www.facebook.com/jack",
  "username": "jack",
  "gender": "male",
  "locale": "en_US",
  "updated_time": "2013-07-23T21:13:23+0000"
}

错误是:

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method

为什么会发生这个错误 - 有人知道吗?

最佳答案

您可以尝试使用 HttpServletRequest。它没有任何问题

  @RequestMapping(value = "/save-profile", method = RequestMethod.POST,consumes="application/json",headers = "content-type=application/x-www-form-urlencoded")
    public @ResponseBody String saveProfileJson(HttpServletRequest request){
       System.out.println(request.getParameter("profileCheckedValues"));
        return "success";
    }

关于jquery - Spring MVC 415 不支持的媒体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18102452/

相关文章:

javascript - 输入数据到可拖动不起作用

jquery - 使用 jQuery 从无序列表中除当前 li 之外的所有 li 添加或删除类

jquery - 如何使用 jquery 获取 :focus ~ . 类 css 选择器?

java - 使用 Spring 或 ScheduledExecutorService 重新填充 ehcache 值

java - 单击红色下划线、@Query、JpaRepository

java - Spring JPA : findByEmail throwing NullPointerException if given email is not found

javascript - 占位符和组合框中的选择

jquery - jQuery 新手,ajax 不工作

jquery - 如何在没有帖子的情况下在 ASP.Net MVC 3 中保留值?

javascript - 如何使用 input type=submit 与 AJAX 表单