我有以下jQuery脚本:
$(document).ready(function() {
$("#resendActivationEmailLink").bind("click", function(event) {
$.get($(this).attr("href"), function() {
$("#emailNotActivated").html("<span>not yet activated. email sent!</span>");
}, "html");
event.preventDefault();
});
});
基本上,当用户单击链接时,将调用以下服务器端方法:
@RequestMapping(value = "/resendActivationEmail/{token}", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody
String resendActivationEmail(@PathVariable("token") String token) {
preferencesService.resendActivationEmail(token);
return "dummy";
}
并且在服务器上执行了一些业务逻辑,但是除了ajax成功或ajax失败之外,客户端/浏览器端的服务器没有真正的结果。
现在我真的不确定我的服务器端方法应该返回什么!
目前它只返回字符串dummy
,但当然这只是暂时的。我应该选择不返回类型(void
)或null
或其他类型吗?
请注意,我可以更改jQuery get方法的数据类型参数。
修改
我已经改变了我的服务器端方法,如下所示:
@RequestMapping(value = "/resendActivationEmail/{token}", method = RequestMethod.GET)
public @ResponseBody void resendActivationEmail(@PathVariable("token") String token) {
preferencesService.resendActivationEmail(token);
}
@ResponseBody
是必需的,因为这是一个ajax调用。
答案 0 :(得分:1)
我假设您从服务器返回JSON(来自您的服务器代码:produce =“application / json”)。
由于你不关心返回什么,即你没有在$ .get之后处理回调函数中的返回值,那么你可以返回“{}”,或者如果你想处理响应你可以使用类似的东西:
{ "success": true }
// or
{ "error": "Error messages here" }
答案 1 :(得分:1)
在这种情况下返回虚拟值没有意义。如果你没有对返回值做任何事情,那么你可以这样做:
@RequestMapping(value="/resendActivationEmail/{token}", method=RequestMethod.GET)
@ResponseStatus(org.springframework.http.HttpStatus.NO_CONTENT)
public void resendActivationEmail(@PathVariable String token) {
preferencesService.resendActivationEmail(token);
}
将会有一个204
响应代码而不是200
,但这应该没问题。