1 状态码415----已经序列化的json出现了错误
AbstractHandlerExceptionResolver.java 186 Handler execution resulted in exception: Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported
clickSubmit : function(e) {
if (this.$('form').valid()) {
var data = this.$('form').serializeJson();
var url= adminRoute + '/url/save';
var self = this;
$.ajax({
type : 'POST',
url : url,
data : data,
dataType : 'json',
success : function(result) {
Swiftpass.Dialog.Tips('操作成功');
},
error : function(XMLHttpRequest,
textStatus, errorThrown) {
Swiftpass.Func
.parseError(XMLHttpRequest.responseText);
},
complete : function(XMLHttpRequest,
textStatus) {
}
});
}
}
传值后,后台接收到的是 'application/x-www-form-urlencoded;charset=UTF-8 类型的,但服务端不支持。
解决方案:
需要在ajax中添加 contentType: 'application/json;charset=utf-8',
之后 又出现了错误。。。。。
2 状态码400----data传入后台的参数结构存在问题
解决方案:
在传参时将将data转换为json字符串,可以使用JSON.stringify() 方法
JSON.stringify() 方法用于将 JavaScript 值转换为 JSON 字符串
最后的代码
$.ajax({
type : 'POST',
url : url,
data : JSON.stringify(data),
dataType : 'json',
contentType: 'application/json;charset=utf-8',
success : function(result) {
Swiftpass.Dialog.Tips('操作成功');
},
以上方案可以使用,但原因我决定的很迷,同样的后台,为什么其他页面就没出现这两个问题?。。。。json都序列化了为什么还要转,而其他的就不用转?就很迷。。。。