Md. Arafat Al Mahmud
Md. Arafat Al Mahmud

Reputation: 3214

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)

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?

Upvotes: 4

Views: 16458

Answers (4)

Siva Anand
Siva Anand

Reputation: 2882

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

Upvotes: 0

Mox Shah
Mox Shah

Reputation: 3015

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.

Upvotes: 0

Mai Tan
Mai Tan

Reputation: 31

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

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

Upvotes: 0

Abhishek Nayak
Abhishek Nayak

Reputation: 3748

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:

Upvotes: 1

Related Questions