jquery – Spring MVC 415不支持的介质类型

前端之家收集整理的这篇文章主要介绍了jquery – Spring MVC 415不支持的介质类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用的是 Spring 3.2,并尝试使用ajax的post请求来提交一个json对象数组.如果这是相关的,我逃脱了所有的特殊字符.

我的HTTP状态为415.

我的控制器是:

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

jquery是:

  1. jQuery("#save").click(function () {
  2. var profileCheckedValues = [];
  3. jQuery.each(jQuery(".jsonCheck:checked"),function () {
  4. profileCheckedValues.push($(this).val());
  5. });
  6. if (profileCheckedValues.length != 0) {
  7. jQuery("body").addClass("loading");
  8. jQuery.ajax({
  9. type: "POST",contentType: "application/json",url: contextPath + "/sample/save-profile",data: "profileCheckedValues="+escape(profileCheckedValues),dataType: 'json',timeout: 600000,success: function (data) {
  10. jQuery('body').removeClass("loading");
  11. },error: function (e) {
  12. console.log("ERROR: ",e);
  13. jQuery('body').removeClass("loading");
  14. }
  15. });
  16. }
  17. });

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

  1. {
  2. "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"
  3. }

错误是:

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

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

解决方法

你可以尝试使用HttpServletRequest.它没有任何问题
  1. @RequestMapping(value = "/save-profile",consumes="application/json",headers = "content-type=application/x-www-form-urlencoded")
  2. public @ResponseBody String saveProfileJson(HttpServletRequest request){
  3. System.out.println(request.getParameter("profileCheckedValues"));
  4. return "success";
  5. }

猜你在找的jQuery相关文章