jquery ajax代码
$.ajax({ type:"post", url:"/webswmm/runModel", dataType:'json', contentType:"application/json;charset=UTF-8", data:{name:'goatling'}, async:true, success:function (data) { removeLoading('test'); showAlertDiologue("success","run"); resultUrl=data; }, error:function () { removeLoading('test'); showAlertDiologue("fail","run"); } });
@RequestMapping("/webswmm/runModel") @ResponseBody public JSONArray runModel(@RequestBody JSONObject jsonObject) { return dataService.runModel(jsonObject); }
报错:
JSON parse error: Unrecognized token 'name': was expecting 'null', 'true', 'false' or NaN; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'name': was expecting 'null', 'true', 'false' or NaN at [Source: (PushbackInputStream); line: 1, column: 6]
原因:
{name:'goatling'} 这种形式根本不是标准JSON字符串
'{"name":"goatling"}' 这个才是标准JSON字符串
改正后:
$.ajax({ type:"post", url:"/webswmm/runModel", dataType:'json', contentType:"application/json;charset=UTF-8", data:'{"name":"goatling"}', async:true, success:function (data) { removeLoading('test'); showAlertDiologue("success","run"); resultUrl=data; }, error:function () { removeLoading('test'); showAlertDiologue("fail","run"); } });
这样才能成功在后台接收到。 我的是因为
body.bPointInfo.requestInfo.body:undefined使用@RequestBody注解直接转实体时报错