【问题标题】:AJAX POST request to Spring MVC Controller not working对 Spring MVC 控制器的 AJAX POST 请求不起作用
【发布时间】:2015-03-21 11:50:39
【问题描述】:

我遇到了错误:

加载资源失败:服务器响应状态为 415 (不支持的媒体类型)

我的代码的AJAX部分如下:

$.ajax({ 
    url: '/authentication/editUser',    
    type: "POST", 
    contentType: "application/json",
    data: JSON.stringify(requestObj), //Stringified JSON Object

    success: function(resposeJsonObject) {
       //
    }   
});

以及控制器的处理方法:

@RequestMapping(value = "/editUser", method = RequestMethod.POST, 
    headers = {"Content-type=application/json"})
@ResponseBody
public  EditUserResponse editUserpost(@RequestBody EditUserRequest editUserRequest) {
    System.out.println(editUserRequest);
    return new EditUserResponse();
}

如何解决错误?

【问题讨论】:

  • 尝试在标题中传递内容类型。
  • 它在 ajax 调用中作为 contentType: "application/json" 完成
  • 我是说它在标题中单独传递 headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } ,类似这样。
  • 还将这一行 contentType: "application/json", 替换为 'dataType': 'json',。
  • 你有杰克逊依赖吗?

标签: jquery ajax json spring spring-mvc


【解决方案1】:

在您的 AJAX 函数的 beforeSend 处理程序中手动设置 Content-Type 和 Accept,并删除您的 spring 控制器处理程序方法的标头以使其工作如下:

AJAX 函数:

$.ajax({
    url: '/authentication/editUser',
    type: 'POST',
    data: JSON.stringify(requestObj), //Stringified Json Object
    beforeSend: function(xhr) {
        xhr.setRequestHeader("Accept", "application/json");
        xhr.setRequestHeader("Content-Type", "application/json");
    }
    success: function(resposeJsonObject){}
});

和控制器处理方法如:

@RequestMapping(value = "/editUser" , method = RequestMethod.POST)
public @ResponseBody EditUserResponse editUserpost(@RequestBody EditUserRequest editUserRequest){
   System.out.println(editUserRequest);
   return new EditUserResponse();
}

另见:

【讨论】:

    【解决方案2】:

    我认为您应该尝试像这样删除控制器中的标头:

    @RequestMapping(value = "/editUser" , method = RequestMethod.POST)
    

    【讨论】:

      【解决方案3】:

      更改您的 AJAX 调用,添加标头:

      headers: { 
          'Accept': 'application/json', 
          'Content-Type': 'application/json' 
      }
      

      您的整体 AJAX 调用应如下所示:

      $.ajax({ 
          url: '/authentication/editUser',    
          type: "POST", 
          headers: { 
              'Accept': 'application/json', 
              'Content-Type': 'application/json' 
          }
          dataType: "json",
          data: JSON.stringify(requestObj), //Stringified JSON Object
      
          success: function(resposeJsonObject) {            
              //
          }
      });
      

      @RequestBody 使用 Content-Type 标头来确定要发送的数据格式。

      【讨论】:

      • 同样的错误仍然存​​在 - POST localhost:8080/authentication/editUser 415(不支持的媒体类型)
      • 你添加dataType: "json",了吗?
      • 那么它应该可以工作,尝试从HeaderMapping 中删除headers = {"Content-type=application/json"}。
      【解决方案4】:

      试试这个..我改变了控制器和 jquery.. 希望它能工作

      $.ajax({ 
              url: '/authentication/editUser',    
              type: "POST", 
              contentType: "application/json",
              data: $("#FormName").serialize()
      
              success: function(respose) {
                 //
              }   
          });
      

      以及控制器的处理方法:

        @RequestMapping(value = "/editUser", method = RequestMethod.POST, 
              headers = {"Content-type=application/json"})
      // removed @RequestBody 
          @ResponseBody
          public  EditUserResponse editUserpost(EditUserRequest editUserRequest) {
              System.out.println(editUserRequest);
              return new EditUserResponse();
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-03-04
        • 2021-08-09
        • 2018-08-06
        • 1970-01-01
        • 2016-11-28
        • 1970-01-01
        • 2016-11-05
        相关资源
        最近更新 更多