Spring MVC ajax @ResponseBody

时间:2019-02-26 07:48:13

标签: ajax spring-mvc

伙计!我对ajax请求有麻烦。服务器端有控制器,该控制器应返回一些字符串(@ResponseBody)。结果,当我尝试进入/ajax.html时,执行请求后,状态为500,则显示ModelView文本。

@Autowired
private MsgService msgService;


@RequestMapping("/ajax")
public ModelAndView helloAjaxTest() {
    return new ModelAndView("ajax", "message", "Spring MVC Example with AJAX call");
}

@RequestMapping(value = "/ajaxtest", method = RequestMethod.GET)
public @ResponseBody
String ajaxResponse(){
    String text = "It should be response from DB";
    return text;
}

@RequestMapping(value = "/saw", method = RequestMethod.GET)
public String saw(Model model){
   // model.addAttribute("message",new Message());
    model.addAttribute("msg", msgService.getMessage(1));
    return "saw";
}

和包含我的请求的ajax.jsp:

<!DOCTYPE html>
<html>
<head>
  <TITLE>Spring MVC Example with AJAX call</TITLE>


  <script type="text/javascript"
          src="http://code.jquery.com/jquery-3.1.0.min.js"></script>
  <script type="text/javascript">
    function actAjax() {
      $.ajax({
        type:"GET",
        url : "ajaxtest.html",
        success : function(data) {
          $('#result').html(data);
        }
      });
    }
  </script>

  <script type="text/javascript">
    var intervalId = 0;
    intervalId = setInterval(actAjax, 6000);
  </script>
</head>

<body>
<div align="center">
  <br> <br> ${message} <br> <br>
  <div id="result"></div>
  <br>

</div>
</body>
</html>

0 个答案:

没有答案