【问题标题】:Jquery Ajax post Spring MVC controller?Jquery Ajax 发布 Spring MVC 控制器?
【发布时间】:2017-12-09 01:39:15
【问题描述】:

我正在尝试使用 Spring Boot,并希望对 Spring 控制器进行 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>

【问题讨论】:

  • 你也可以分享属性文件吗?
  • 测试网址。我认为问题是页面没有定义。清理代码并重新编译。
  • 你能展示你完整的控制器类吗?

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


【解决方案1】:

你需要定义你的视图解析器来区分 web 和 rest 调用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 2016-06-12
    相关资源
    最近更新 更多