简体   繁体   中英

Spring MVC Ajax Request not active in controller class

I am new at spring MVC .I am trying to do ajax request on spring MVC. This seem when ajax call act controller method doesnt response.But I didnt understand why it is. But I am getting this error on browser console:

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

Here My method in controller class:

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

Here My Ajax call in index.jsp:

$(function(){

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

        });

What can I do fix this error?

Thanks

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM