使用ajax调用@Postmapping不起作用

时间:2017-07-26 15:26:43

标签: spring-mvc spring-4

具有新实施的控制器

@Controller
@RequestMapping("/something.do")
public class ExampleController {

        @GetMapping
        public ModelAndView getSomething(HttpServletRequest request, ModelMap model) {
            //do something
        }

        @PostMapping(params = { "param1=user1", "process=update" })
        public ModelAndView updateuser1(HttpServletRequest request, @ModelAttribute("userData") UserData data) {
            // do something
        }

        @PostMapping(params = { "param1=user2", "process=update" })
        public ModelAndView updateuser1(HttpServletRequest request, @ModelAttribute("userData") UserData data) {
            // do something
        }

    }

Ajax调用

$.ajax({
		type : "POST",
		url : "something.do?&param1=user1&process=update",
		success : function(data) {
			 // do something
			});
		},
		error : function() {
			// do something
		},
		data : $("#form").serialize(),
		timeout : 50000
	});

当我尝试调用其中一个post方法时,它给我的POST方法不支持异常 org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法'POST'

但是我的旧实现工作正常。

旧的实施:

@Controller
@RequestMapping("/something.do")
public class ExampleController {

    @RequestMapping(method = RequestMethod.GET
    public ModelAndView getSomething(HttpServletRequest request, ModelMap model) {
        //do something
    }

    @RequestMapping(method = RequestMethod.POST, params = { "param1=user1", "process=update" })
    public ModelAndView updateuser1(HttpServletRequest request, @ModelAttribute("userData") UserData data) {
        // do something
    }

    @RequestMapping(method = RequestMethod.POST, params = { "param1=user2", "process=update" })
    public ModelAndView updateuser1(HttpServletRequest request, @ModelAttribute("userData") UserData data) {
        // do something
    }

}

如果我以正确的方式使用@PostMapping,有人可以告诉我吗? Ajax调用在调用PostMapping时失败。

0 个答案:

没有答案