简体   繁体   English

对Spring MVC Controller的AJAX POST请求无法正常工作

[英]AJAX POST request to Spring MVC Controller not working

I am facing the error: 我面临错误:

Failed to load resource: the server responded with a status of 415 (Unsupported Media Type) 无法加载资源:服务器响应状态为415(不支持的媒体类型)

AJAX portion of my code is as follows: 我的代码的AJAX部分如下:

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

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

And the controller's handler method: 和控制器的处理程序方法:

@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();
}

How to resolve the error? 如何解决错误?

Manually set Content-Type and Accept in beforeSend handler of your AJAX function, and remove headers of your spring controller handler method to make it work like: 在AJAX函数的beforeSend处理程序中手动设置Content-TypeAccept ,并删除spring controller handler方法的标题,使其工作方式如下:

AJAX function: 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){}
});

and controller handler method like: 和控制器处理程序方法如:

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

See Also : 另见

Change your AJAX call, add headers: 更改您的AJAX调用,添加标题:

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

Your overall AJAX call should look like this: 您的整体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) {            
        //
    }
});

The Content-Type header is used by @RequestBody to determine what format the data being sent. @RequestBody使用Content-Type标头来确定要发送的数据的格式。

我认为你应该尝试删除控制器中的标头,如下所示:

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

Try this.. i changed in controller and jquery.. hope it works 试试这个..我改变了控制器和jquery ..希望它的工作原理

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

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

And the controller's handler method: 和控制器的处理程序方法:

  @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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM