1. 前端contentType:"application/json",
1) 前端请求
$.ajax({
type: 'POST',
url: contextPath + '/XXX/update',
data: JSON.stringify(XXXX),
contentType:"application/json",
success: function(data){
stopLoad();
if(!identityCheck(data)) return;
if(data.resultCode == 'XXX000000'){
//Ewin.alert("修改成功!");
$("#brid").bootstrapTable('refresh');
location.href = contextPath + "/page/XXX/list";
}else{
Ewin.alert(data.bizMsg);
}
},
error: function(){
stopLoad();
}
});
}else{
//新增
$.ajax({
type: 'POST',
url: contextPath + '/XXX/add',
data: JSON.stringify(XXXX),
contentType:"application/json",
success: function(data){
stopLoad();
if(!identityCheck(data)) return;
if(data.resultCode == 'NRC000000'){
//Ewin.alert("新增成功!");
$("#brid").bootstrapTable('refresh');
location.href = contextPath + "/page/XXX/list";
}else{
Ewin.alert(data.bizMsg);
}
},
error: function(){
stopLoad();
}
});
}
2)后端接收:
@RequestMapping(value="/add", method = RequestMethod.POST)
public Object addXXX(@RequestBody JSONObject obj){
xxxDef XXX= JSON.toJavaObject(obj,XXX.class);
Map<String, Object> stringObjectMap = xxxDefService.XXX(obj, user);
return stringObjectMap;
}
@RequestMapping(value="/update", method = RequestMethod.POST)
public Object update(@RequestBody JSONObject obj){
xxxDef xxxDef = JSON.toJavaObject(obj,xxxDef.class);
Map<String, Object> stringObjectMap = xxxService.updateXXX(obj, user);
return stringObjectMap;
}
2. 前端contentType:"application/x-www-form-urlencoded; charset=UTF-8",
1)前端请求:
$.ajax({
type: 'POST',
url: ctx + '/app/update',
data: {
"xxx":self.xxx(),
"patternType":self.patternTypeAdd(),
"keyWord":self.xxx(),
"keyContent":self.xxxx()
},
contentType:"application/x-www-form-urlencoded; charset=UTF-8",
success: function(data){
stopLoad();
if(!identityCheck(data)) return;
if(data.resultCode == 'NRC000000'){
//Ewin.alert("修改成功!");
Ewin.alert("修改成功!");
$("#xxxx").modal('hide');
$("#brid").bootstrapTable('refresh');
}else{
Ewin.alert();
}
},
error: function(){
stopLoad();
}
});
}else{
//新增
$.ajax({
type: 'POST',
url: ctx + '/app/add',
//data: JSON.stringify(xxxx),
data: {
"xxx":self.xxx(),
"xxx":self.xxx(),
"xxx":self.xxxx()
},
contentType:"application/x-www-form-urlencoded; charset=UTF-8",
success: function(data){
stopLoad();
if(!identityCheck(data)) return;
if(data.resultCode == 'NRC000000'){
Ewin.alert("新增成功!");
$("#xxx").modal('hide');
$("#brid").bootstrapTable('refresh');
//location.href = ctx + "xxxx/list";
app.doQuery();
}else{
Ewin.alert(data.bizMsg);
}
},
error: function(){
stopLoad();
}
});
}
2)后端接收:
@RequestMapping(value = "/add")
public Object addxxxx(HttpServletRequest request){
User user = (User)request.getSession().getAttribute("user");
if(user == null){
return getIllegalResponse();
}
String xxxx= request.getParameter("xxx");
String xxx= request.getParameter("xxx");
String xxx= request.getParameter("xxx");
........................
}
@RequestMapping(value = "/update")
public Object updatexxxx(HttpServletRequest request){
User user = (User)request.getSession().getAttribute("user");
if(user == null){
return getIllegalResponse();
}
String xxx= request.getParameter("xxx");
String xxx= request.getParameter("xxx");
String xxx= request.getParameter("xxx");
String xxx= request.getParameter("xxx");
...................................
}
参考:
1) HttpRequest中常见的四种ContentType:
https://www.cnblogs.com/htoooth/p/7242217.html
https://www.cnblogs.com/hejingjing/p/6956906.html
https://www.cnblogs.com/xiaozong/p/5732332.html
2) Http中Content-Type的详解:
https://blog.csdn.net/danielzhou888/article/details/72861097
3) 常用HTTP contentType与后端处理方式
https://www.jianshu.com/p/dc6873d5fa3d
https://www.jianshu.com/p/dc6873d5fa3d
4)
AJAX对于JAVA接收HttpServletRequest和json字符串两种方式的传递写法
https://blog.csdn.net/wohaqiyi/article/details/79284106
5) ContentType
https://blog.csdn.net/u013789656/article/details/80927900
6) content-type的作用以及类型
https://blog.csdn.net/hw_hi/article/details/78529040
7) ajax请求中contentType与dataType区别
https://blog.csdn.net/john1337/article/details/60867183
8) HTTP Content-type 对照表 | content type
https://www.sojson.com/httpcontent.html
9) 对比获取文件Content-Type的四种方法
https://cloud.tencent.com/developer/article/1336314
10) JSP内置对象:设置content-type属性
https://www.2cto.com/kf/201707/656843.html