Jquery Ajax发布Spring MVC控制器?

时间:2017-07-05 11:45:44

标签: jquery ajax spring spring-mvc spring-boot

我正在尝试使用Spring Boot,并希望对Spring Controller进行Ajax POST。我是从POSTMAN测试的,但后来我一直收到这个错误。

{
    "timestamp": 1499255141424,
    "status": 404,
    "error": "Not Found",
    "message": "/WEB-INF/view/jsondata.jsp",
    "path": "/jsondata"
}

我希望它在我的索引控制器中查找jsondata方法。但它在我的观点中寻找jsondata.jsp。

@RequestMapping(value = "/jsondata", method = RequestMethod.POST)
    public void getfeeddata(@RequestBody String info)
    {
        System.out.println(info);
    }

查看 -

<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html lang="en">
<body>
<head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            $('#submit').click(function ()
            {
                var info =[];
                info.push("JsonPostdata");
                $.ajax({
                    type: "post",
                    url: "/jsondata", 
                    data: JSON.stringify(info),
                    success: function(msg){      
                        console.log("success");
                    }
                });
            });

        });
    </script>
</head>
    <div>
        <div>
            <h1>Spring Boot JSP Example</h1>
            <form >
              <input type="submit" id ="submit" value="Not clicked">

            </form>
            <h2>Hello ${message}</h2>

            Click on this <strong><a href="next">link</a></strong> to visit another page.
        </div>
    </div>


</body>
</html>

1 个答案:

答案 0 :(得分:0)

您需要定义视图解析器以区分网络和其余呼叫:

@Bean  
public InternalResourceViewResolver viewResolver() {  
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();  
    resolver.setPrefix("/WEB-INF/view/");  
    resolver.setSuffix(".jsp");
    return resolver;  
}