在使用ajax请求的时候,需要返回json格式的数据。
var param = {"phoneNumber": phoneNumber};
$.ajax({
type: "POST",
url: "bindingController.do?sendMessage",
dataType: "JSON",
async: false,
data: param,
success: function (data) {
if (data.flag == "true") {
$.mobAlert('验证码发送成功,请注意查收!');
} else {
$.mobAlert('验证码发送失败,请稍后重试!');
}
}, error: function (error) {
$.mobAlert('验证码发送失败,请稍后重试!');
},
});
需要从Controller返回json格式的数据:
<span style="font-size:18px;"><span style="font-size:18px;"> ObjectMapper objectMapper =new ObjectMapper();
Map<String,Object> map=new HashMap<>();
map.put("flag","true");
String json = objectMapper.writeValueAsString(map);
PrintWriter out = response.getWriter();
out.write(json);
</span></span>
这样就可以返回json格式的数据了。