qin528032307
qinhaozzz
2017-07-22 10:19

spring boot返回json数据,在html通过ajax请求不能获取数据

1
  • spring
  • ajax

1.这是我的controller代码

        @RestController
        public class ComputeController {

    private final Logger logger = Logger.getLogger(getClass());

        @RequestMapping(value = "/test", method = RequestMethod.GET)
        public List<String> test() {
                ServiceInstance instance = client.getLocalServiceInstance();

        List<String> list = new ArrayList<>();
        list.add("hello");
        list.add("world");
        logger.info("/test test" + instance.getHost() + ",service_id:" + instance.getServiceId() + ",result:" + list.toString());
        return list;
            }
        }

2.这里是我的html

    <!DOCTYPE html>
    <html lang="en">
        <head>
    <meta charset="UTF-8">
    <title>INDEX</title>
    <script src="jquery-1.8.0.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#btn").click(function () {
                alert("start");
                $.get("http://localhost:4444/add?a=12&b=3", function (data) {
                    $("div").html(data);
                });
                alert("end");
            });
        });
        </script>
        </head>
        <body>
        <h2>INDEX</h2>
        <button id="btn">ajax</button>
        <div id="div"></div>
        </body>
    </html>

问题:在jquery中,alert("start")和alert("end")都会正常执行,但是ajax的回调函数不能正常执行。

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

5条回答