ajax提交到Controller时,报错:
WARN [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-18) Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized token 'img': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@53bcac4b; line: 1, column: 5]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'img': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@53bcac4b; line: 1, column: 5]
解决方法:
在ajax请求中,加入dataType参数,并且data用JSON.stringify格式化
var params = { "img": img, "fid": fid };
$.ajax({
async: false,
type: "POST",
contentType: 'application/json',
dataType: 'json',
url: "stampCustomerContractSubmit",
data: JSON.stringify(params),
success: function (data) {
//...
}
})
另外:
contentType设置成 'application/json',
否则可能 http请求出现:415错误Unsupported Media Type