Spring MVC 控制器不会拦截 Ajax POST

Spring MVC controller does not intercept Ajax POST

本文关键字:Ajax POST MVC 控制器 Spring      更新时间:2023-09-26

我有一个javascript函数:

var RestPost = function() {
    $.ajax({
        type : 'POST',
        url : '/bollo/search/details',
        dataType : 'json'
    });
};

以及设置在同一 URL 上的控制器

@RestController
@RequestMapping(value = "/bollo/search/details")
public class BolloRestController
{
    @RequestMapping(method = RequestMethod.POST)
    public String details()
    {
        System.out.println("method reached");
        return "bollo/ricerca_bolli.html";
    }
}

我搜索过很多网站,有很多例子,但我并没有真正明白我做错了什么,因为 URL 是相同的,请求也是相同的。

在Chrome的控制台中调试代码时,它总是得到相同的错误:

POST http://localhost:9080/bollo/search/details 404(未找到)

有谁知道为什么?

AppContextRootName:您的应用程序上下文根目录

var RestPost = function() {
        $.ajax({
            type : 'POST',
            url : '/AppContextRootName/bollo/search/details',
            dataType : 'json'
        });
    };

在 URL 中添加项目名称/bollo/search/details,然后它不会给出 404 错误。

就像你用 SpringWS 的名字制作了 Web 项目一样,使用 SpringWS/bollo/search/details 来触发 Ajax 请求。这是我的机箱端口是8085。

<script>
var RestPost = function() {
    $.ajax({
        type : 'POST',
        url : 'http://localhost:8085/SpringWS/bollo/search/details',
        dataType : 'json'
    });
};
RestPost();
</script>