關於ajax方法的dataType屬性和成功後回撥函式遇到的幾點問題
ajax的dataType屬性是定義Controller中返回值的型別,兩者需要保持一致,如果不一致需要變換型別。
比如 :
前臺js中
$.ajax({
type: "POST",
data : {
name: name,
code: code,
summary: summary
},
dataType: "JSON",
async: false,
url: contextPath + "/A05/A05A01/A05A01A04/insert",
success: function(data) {
alert("申請成功,請耐心等待上級批覆");
App.formPost("/main");
}
});
後臺controller中 1 要有ResponseBody註解;2 要把Map型別轉換成Json的格式JSONString的型別
@RequestMapping(value = "/A05/A05A01/A05A01A04/insert")
@ResponseBody
public String insertApplication (String code, String summary, String name,
HttpServletRequest request, ModelAndView mv, HttpSession session) {
Map<String,Object> map = new HashMap<String,Object>();
int insert = a05Service.insert(code, summary);
if (insert > 0) {
logger.info("username" + name + " :轉正申請操作成功,請耐心等待上級審批");
map.put("resultCode", 1);
return JSON.toJSONString(map);
} else {
setResponseError(mv, "轉正申請不成功,請重新申請!");
map.put("resultCode", 0);
return JSON.toJSONString(map);
}
}