輸出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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章