输出json格式数据

在使用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格式的数据了。



发布了121 篇原创文章 · 获赞 10 · 访问量 11万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章