【问题标题】:Spring MVC Ajax Request not active in controller classSpring MVC Ajax 请求在控制器类中不活动
【发布时间】:2015-07-22 19:36:04
【问题描述】:

我是 Spring MVC 的新手。我正在尝试在 Spring MVC 上执行 ajax 请求。这似乎是当 ajax 调用行为控制器方法没有响应时。但我不明白为什么会这样。 但我在浏览器控制台上收到此错误:

Failed to load resource: the server responded with a status of http://localhost:8080/spapp/ajaxtest.html 404 (Not Found) 

这是我在控制器类中的方法:

@RequestMapping(value = "/ajaxtest", method = RequestMethod.POST)
public @ResponseBody String ajaxtest() {
    String result = "This is Ajax text from ajaxTest Method";
    return result;
} 

这里是我在 index.jsp 中的 Ajax 调用:

$(function(){

            $(document).on("click","button#save",function(){
                $.ajax({
                    type:"POST",
                    url:"ajaxtest.html",
                    data:{test:"test"},
                    success:function(response){
                        $("div#response").html(response);
                    }
                });
            });

        });

我该如何解决这个错误?

谢谢

【问题讨论】:

    标签: java jquery ajax spring spring-mvc


    【解决方案1】:

    尝试使用url:"ajaxtest",您不必将.html 放在末尾,因为您已经声明了端点value = "/ajaxtest"

    【讨论】:

    • 这对我没有帮助:(
    • 那你还有一个问题。
    • 我不知道,我认为问题出在控制器类或 Java 配置中。尝试使用完整的 url:http://127.0.0.1:8080/ajaxtest(用你的端口更改)。
    【解决方案2】:

    我找到了解决方案。我在 ajax 调用中删除了type="POST"。在它运行之后。

    【讨论】:

    • 这表明您在问题中包含的控制器代码不是您实际运行的代码。
    • 删除type="POST" 后,jQuery 默认使用 GET 并且您的控制器被注释为仅响应 POST。仅此更改无法“修复”您的代码。
    猜你喜欢
    • 2011-10-20
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 2021-08-09
    • 2017-02-12
    • 2016-05-17
    • 2012-05-30
    • 1970-01-01
    相关资源
    最近更新 更多