今日坑,今日毕。明天还有新的坑。
问题:
通过ajax向后台传数据 后台处理完后返回数据给ajax提示找不到对象。
大概是这样的:
页面:
$.ajax({
url : url,
data : data,
type : "post",
dataType : "json",
async : false,
success : function(d){
if(d.code == Global.WEBRESULT_SUCCESS){
//......
}
}
});
平平无奇的前端。
后台接收:
//@ResponseBody
@RequestMapping(value = "xxx", method = RequestMethod.POST)
public WebResult xxx(HttpServletRequest request, HttpServletResponse response) {
String bmh=request.getParameter("bmh");
//return null;
}
平平无奇的后台。
解决:
忘记添加@ResponseBody了。答案写在题面上!就是注释掉的那一行!
呵呵呵。
@ResponseBody
@RequestMapping(value = "xxx", method = RequestMethod.POST)
public WebResult xxx(HttpServletRequest request, HttpServletResponse response) {
String bmh=request.getParameter("bmh");
//return null;
}
分析:
引自:https://blog.csdn.net/qq_21875331/article/details/77849469
经分析是请求没有返回状态码,这是因为我用的是SpringMVC框架,前后端使用JSON传递数据,因为返回的是对象,而忘记了添加
@ResponseBody
所以 Spring对我的返回值进行了映射,但是映射结果又对应不到视图,所以返回了404
拓展,关于@Responsebody:
参考:https://www.cnblogs.com/daimajun/p/7152970.html
一言以蔽之:
在使用@RequestMapping后,返回值会被解析为跳转路径;但是加上@ResponseBody后,返回结果不会解析为跳转路径,而是直接写入Http response body中。
所以这里才会提示:找不到资源(将返回值解析成了跳转路径,但此时没有对应的资源)。