我正在使用Spring 3.2,并尝试使用ajax发布请求来提交json对象数组。如果相关,我转义了所有特殊字符。
我的HTTP状态为415。
我的控制器是:
@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。它没有任何问题
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"; }