天天看點

Ajax傳值SpringMVC擷取為Null的問題【直接上代碼】:

前台使用Ajax請求,背景使用SpringMVC接收請求參數,但是每次都為null,度娘找了半天,最後發現是自己太大意了~~~也是經驗不夠!

【直接上代碼】:

前端Ajax請求代碼:

$.ajax({
        type : "post",
        url : "<c:url value='/changeTaskStatus.do'/>",
        data : {
            "taskStatus":"1",
            "taskName":"eat"
        },
        dataType : "JSON",
        contentType : "application/json",
        success : function(data) {
            console.log(data)
        },
        error : function(data, XMLHttpRequest, textStatus,errorThrown) {
        }
    });
           

背景Java代碼

@RequestMapping("/changeTaskStatus.do")
public @ResponseBody Map<String, String> changeTaskStatus(HttpServletRequest request, String taskStatus, String taskName) {
    Map<String, String> map = new HashMap<String, String>();
    System.out.println(request.getParameter("taskStatus"));
    if (StringUtils.isEmpty(taskStatus) || StringUtils.isEmpty(taskName)) {
        map.put("code", "106");
        map.put("msg", "參數為NULL");
    } else {
        Integer result = ;
        if (result == ) {
            map.put("code", "104");
            map.put("msg", "程式運作成功");
        } else {
            map.put("code", "105");
            map.put("msg", "程式運作失敗");
        }
    }
    return map;
}
           

運作結果

Ajax傳值SpringMVC擷取為Null的問題【直接上代碼】:

解決辦法

去掉Ajax請求中的

contentType : "application/json"

參考:http://blog.csdn.net/blueheart20/article/details/45174399