[英]jQuery.get from Spring RestController
How can should I send the data from Spring controller to the client? 我应该如何将数据从Spring控制器发送到客户端? Do I need to wrap it as Json or is there an easier way?
我需要将其包装为Json还是有更简单的方法?
(noob in web. So, please bear with me) (网络上的菜鸟。所以,请忍受我)
@RequestMapping("/abc")
@RestController
public class ListController {
@RequestMapping(value = "/d", method = RequestMethod.GET)
public StringOrJson? getData() {
return "myData";
}
On the client: 在客户端上:
function checkBoxToggled(){
$(document).get('abc/d', function( data ) {
alert('Data Loaded2:' + data );
});
}
It will be great if you declare the @RequestMapping as follows: 如果按如下所示声明@RequestMapping,那就太好了:
@RequestMapping(value = "/d", method = RequestMethod.GET, produces = "application/json")
You can return simple String response. 您可以返回简单的String响应。
Or you can always reutrn JSONObject as follows: 或者,您始终可以按以下方式重新引用JSONObject:
return new JSONObject("{'data':'myData'}");
In that case the return value in the method signature should be replaced to JSONObject . 在这种情况下,方法签名中的返回值应替换为JSONObject 。
You just need to modify your Controller to: 您只需要将Controller修改为:
@RequestMapping("/abc")
@Controller
public class ListController {
@RequestMapping(value = "/d", method = RequestMethod.GET)
public String getData() {
return "myData";
}
}
And the client should be: 客户应该是:
jQuery.get("abc/d", function (data) {
alert('Data Loaded2:' + data );
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.