'Mapping Ajax Request in Spring MVC: getting error 405 method not allowed

After making the ajax call I am getting error 405 method not allowed. I am using Spring 3.0.1, Spring-web 3.0.1.

Here is the controller mapping

@Controller public class AjaxController {

@RequestMapping(value = "/ajaxaction",
       method = RequestMethod.POST,
                 headers ="content-type=application/json")
public @ResponseBody Collection<Employee> serveAjaxRequest(@RequestBody ReqParam reqParam){
    List<Employee> empList = new ArrayList<Employee>();
    System.out.println("Req obj:: " + reqParam.getA() + " " + reqParam.getB()
            + " " + reqParam.getC() + " " + reqParam.getD() + " " + reqParam.getE());
    Employee e1 = new Employee();
    e1.setFirstName("Vaibhav");
    e1.setLastName("Raj");
    e1.setEmail("[email protected]");
    e1.setTelephone("1111111111");
    e1.setReturnMessage("Message one!!");
    Employee e2 = new Employee();
    e1.setFirstName("Ajay");
    e1.setLastName("Singh");
    e1.setEmail("[email protected]");
    e1.setTelephone("2222222222");
    e1.setReturnMessage("Message two!!");
    empList.add(e1);
    empList.add(e2);
    return empList;
}

)

and the Jquery code for ajax call:

function : submitAjax(){

$('#g').bind('click', function(evt) {
 alert($('form').serialize());
 formData = $('form').serialize();
 $.ajax({
 url: "/ajaxaction.html",
 type: 'POST',
 dataType: 'json',
 data: formData,
 success: function(data) {
          alert(data);
          },
 error: function(){
    alert("Error!!");
    }
   });

});


Solution 1:[1]

From what the error tells, this may be your problem:

@RequestMapping(value = "/ajaxaction", method = RequestMethod.POST, headers ="content-type=application/json")

You can use firebug to check the request headers. Is there a header for "content-type"?

Solution 2:[2]

Try to set request content type using contentType: 'application/json' in ajax options.

$('#g').bind('click', function(evt) {
alert($('form').serialize());
formData = $('form').serialize();
$.ajax({
url: "/ajaxaction.html",
type: 'POST',
dataType: 'json',
data: formData,
contentType: 'application/json',
success: function(data) {
      alert(data);
      },
error: function(){
  alert("Error!!");
  }
   });

});

Solution 3:[3]

When using JSON response from Spring controller you need to define Accept header. By default Spring defines Accept header to html. Add header:

headers = "Accept=application/json"

Modify your method annotation and change it like:

@RequestMapping(value = "/ajaxaction",
       method = RequestMethod.POST,
                 headers ="Accept:*/*")
public @ResponseBody Collection<Employee> serveAjaxRequest(@RequestBody ReqParam reqParam)

Solution 4:[4]

Hi I got the same error and I fixed this by changing my Ajax call from get to post because I mentioned method as RequestMethod.POST in my @RequestMapping annotation same as you.

but I was making a get call to that method. check your ajax call

Solution 5:[5]

Enable Cross Origin Requests for a RESTful Web Service, take help from here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hoàng Long
Solution 2 Subin Sebastian
Solution 3 Viral Patel
Solution 4 Naga Srinu Kapusetti
Solution 5 Niranjan Kumar