目录

ajax传单个值到后端

ajax传单个值到后端


前端代码如下(示例):

$.ajax({
	url: "<%=basePath%>/cancel",
	type: "POST",
	async: true,//是否异步
	// contentType: "application/json;charset=UTF-8",  此处一定要注释掉
	data:{user:"username"},
	dataType: 'json',
	success: function (data) {
		console.log(data);
	}
});

后端代码如下(示例):

@RequestMapping("cancel")
@ResponseBody
public int cancel(@RequestParam("user") String user) {
	System.out.println(user);
	return managementService.cancel(user);//将值传入Service层
}

当后端有对应的实体类时,用@RequestBody注释 去解析json值

ps: 当传输类型为get时,@RequestBody不可用

但当传普通值时,用@RequestParam注释 去解析json值

但是它只支持Content-Type: 为 application/x-www-form-urlencoded编码的内容

而且,ajax默认的Content-Type 为 application/x-www-form-urlencoded