4
votes

I am facing the error:

Failed to load resource: the server responded with a status of 415 (Unsupported Media Type)

AJAX portion of my code is as follows:

$.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?

4
Try passing content type in headers.Mox Shah
its done in the ajax call as contentType: "application/json"Md. Arafat Al Mahmud
I'm saying it pass it separately in headers as headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } , something like this.Mox Shah
Also replace this line contentType: "application/json", with 'dataType': 'json',.Mox Shah
Do you have jackson dependencies?minion

4 Answers

1
votes

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 function:

$.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:

0
votes

I think you should try to remove header in controller like this:

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

Change your AJAX call, add headers:

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

Your overall AJAX call should look like this:

$.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.

0
votes

Try this.. i changed in controller and jquery.. hope it works

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