简体   繁体   English

响应体上的Spring MVC + Ajax

[英]Spring mvc + ajax on Responsebody

I have a trouble understand how @Responsebody work. 我很难理解@Responsebody的工作方式。

So I have a page call browse, and in the page, I have a button so that whenever the client click on the button, the page will load more data from the server. 因此,我有一个页面调用浏览器,并且在页面中,我有一个按钮,以便每当客户端单击该按钮时,该页面将从服务器中加载更多数据。

current page URL: /myapp/browse button: Load more div: This is my jquery script: 当前页面URL:/ myapp / browse按钮:加载更多div:这是我的jquery脚本:

$(document).ready(function(){
  $("#loadmore").click(function() {
    $.getJSON('browse', function(jd) {
        $("#result").append(jd);
    });
  });
});

And this is my server code: 这是我的服务器代码:

@RequestMapping(value="/loadmore", method=RequestMethod.GET)
@ResponseBody
public String loadMore() {
    return "loadit";
}

So I expected that the page will stay as same as it is, and the string will be displayed in the div#result. 因此,我希望页面会保持原样,并且该字符串将显示在div#result中。 However, the string is display in an empty page with the URL: /myapp/loadmore. 但是,该字符串显示在具有URL的空页面中:/ myapp / loadmore。 Can any point out what did I do wrong please? 能指出我做错了什么吗? Thank in advance. 预先感谢。

You have to send your request to your controller; 您必须将请求发送到您的控制器;

$(document).ready(function(){
  $("#loadmore").click(function() {
    $.getJSON('/loadmore', function(jd) {
        $("#result").append(jd);
    });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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