场景
Jquery中使用ajax向SSM后台请求数据时提示:
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Stream closed
实现
出现这种错误是因为前段请求参数和后端接受参数不对应导致。
前端:
$.ajax({
type: 'POST',
url: formAction,
cache: false, //禁用缓存
data:JSON.stringify({"guojiStrart":guojiStrart,"guojiEnd":guojiEnd}),
contentType: "application/json",
dataType: "json",
success: function (result) {
debugger
}
});
后端错误代码:
@ResponseBody
@RequestMapping("/validateIsGuoNeiAll")
public Map<String, Object> validateIsGuoNeiAll(@RequestBody Map<String, Object> param1,@RequestBody Map<String, Object> param2){
Map<String, Object> result = new HashMap<String, Object>();
Object PrintId = param1.get("guojiStrart");
return result;
}
正确代码:
@ResponseBody
@RequestMapping("/validateIsGuoNeiAll")
public Map<String, Object> validateIsGuoNeiAll(@RequestBody Map<String, Object> params){
Map<String, Object> result = new HashMap<String, Object>();
Object PrintId = params.get("guojiStrart");
return result;
}
接受用一个map接收即可。