ajax 报错 400 (Failed to load resource: the server responded with a status of 400 (Bad Request))
来源:互联网 发布:淘宝运营总监年薪 编辑:程序博客网 时间:2024/05/04 22:08
Failed to load resource: the server responded with a status of 400 (Bad Request)
http://localhost:8081/ezsonar/npm/appportmanage/saveEditAppPortManage
报错代码为400的错误的原因:基本上都是前台传的数据格式不正确造成的,但是这个格式如何不正确,就得看如何理解这个数据到底是怎么在传的啦。
写下我的代码报错400,的原因:
前台代码:
$.ajax({url: SUBSYSTEM_APP_NAME + "appportmanage/saveEditAppPortManage",type: "post",contentType: "application/json; charset=utf-8",data: JSON.stringify(data),dataType: "json",success: function (data) {if (data.success) {GMS.success(data.msg);} else {GMS.error(data.msg, 3000);}}});
首先是,前台封装的数据对象的属性什么的,都是和后台的java的model的属性是一致的,这个不是问题的原因。不用担心。
请注意啦,报400错的各位,我说的是我的这个所有属性的前后台对应是OK的,但是你的报400错误的,你确定你的前后台对象确实是所有属性的参数类型都是一致的吗?比如:某个属性前后台 都是List<String>?都是boolean?等等等等。这个是最基本的报错姿势。
你还是最后先检查一下这个问题,再继续看是不是和我的报错原原因是相同的吧。
然后ajax的type和各种参数都是和后台可以 对的上的,也不是问题的原因。
后台的代码:
@RequestMapping(value = "/saveEditAppPortManage")public @ResponseBody JsonResult saveEditRenameDetail (@RequestBody Appportmanage detail) {LOG.debug("---------------AppportmanageController:saveEditAppPortManage---------------");LOG.debug("---------------detail:" + detail + "---------------");return appportmanageService.saveEditAppPortManage(detail);}
其次是,前后台的url也是对上的,model的各个属性也是对上的。然后呢,spring mvc 的注解标签也是没问题的。
本来代码是OK的。还是上面的代码,程序是可以正常工作的,跑起来没问题的。
但是我做了如下修改:
修改了数据的model:原来的 model java 文件就是一些属性s和简单的getter和setter,然后我因业务需求,给原来的model添加了一条属性,当然对应的getter和setter也是添加的,然后又因为需求我添加了一个带参数的构造函数,因为我在其他地方要new这个model的对象。
然后,,,
问题就出现了。
就报 400 的错误码了。
刚刚开始,我以为是属性的添加的问题,但是检查之后,发现不是这个问题。
问题出现在,model java 文件的构造函数上。
解决方法:给model java 文件再添加一个不带参数的构造函数。具体解释在下面。
至于原因,就得看怎么理解spring mvc是怎么在前后台之间传递参数了。
原来我没加带参构造函数的时候,每个model都会默认自带个不带参数的构造函数,然后前台的数据格式封装 和后台的model的属性只要对的上,然后spring mvc在后台 就可以自己根据model,自己去把前台传过来的数据,对应的给装进到@RequestBody Appportmanage detail,这个参数里面,这个实现的前提是,你的model有默认构造函数,不带参数的,然后系统自己去new一个对象,然后自己去把数据给装进去。然后你就可以用了。
因为,我更新了model 的构造函数,若是你不写不带参数的构造函数的话,那么原来model自带的不带参构造函数就木有了,然后到controler层的时候,就是在获得前台的数据,自己去装数据的时候,他不会根据你的带参构造函数去new个你要的model对象出来,so,问题就这么出现了。
然后,遇到今天的这个错误之后,就知道前后台的数据具体是怎么在对应的了。原来我只是以为,只要前后的数据模型一致就可以 了。
现在,又知道了,哦,还和构造函数有关系呢。
我为什么要把空构造函数 给删除了,因为ide提示说这个构造函数没用,然后我就删除了。然后我就悲剧了。哦,西特 !!!
我这个报错的原因还是有点深度的,因为这个报错,可以让你理解到,springMVC的注解是如何工作的,你要是知道什么是IOC的话,也就是spring 里面经常说的依赖注入和控制反转的话,那就对这个错误的理解就更深刻了。
不过,报错 的姿势有很多种,不能因为你的姿势和我的不对,你就踩我的文章一脚,对不对。
哎,桑心,不造,哪个年轻的骚年,狠心的踩了本篇文章一脚,湿胸我是有点难过。
- ajax 报错 400 (Failed to load resource: the server responded with a status of 400 (Bad Request))
- Failed to load resource: the server responded with a status of 400 (Bad Request)
- Failed to load resource: the server responded with a status of 400 (Bad Request)
- Failed to load resource: the server responded with a status of 400 (Bad Request)
- Failed to load resource: the server responded with a status of 400 (Bad Request)
- 关于前台jsp页面Failed to load resource: the server responded with a status of 400 (Bad Request)
- 关于使用springmvc时Failed to load resource: the server responded with a status of 400 (Bad Request)错误可能原因
- 关于使用springmvc时Failed to load resource: the server responded with a status of 400 (Bad Request)错误可能原因
- Failed to load resource: the server responsed with a status of 400 (Bad Request)
- 微信小程序 wx.request 出现 Failed to load resource: the server responded with a status of 400
- Failed to load resource: the server responded with a status of 413 (Request Entity Too Large)
- 上传文件失败 Failed to load resource: the server responded with a status of 413 (Request Entity Too Large)
- Failed to load resource: the server responded with a status of 500 (Internal Server Error)
- Failed to load resource: the server responded with a status of 500 (Internal Server Error)
- failed to load resource the server responded with a status of 500 (internal server error)
- Failed to load resource: the server responded with a status of 500 (Internal Server Error)
- failed to load resource the server responded with a status of 500 (internal server error)
- Spring MVC json报406错误的解决办法 Failed to load resource: the server responded with a status of 406
- cms网站模板上传安装教程
- 常驻性广播使用注意
- Linux中的文件特殊权限
- nginx配置和自己理解
- Tomcat6.0的安装与配置(手把手教你)
- ajax 报错 400 (Failed to load resource: the server responded with a status of 400 (Bad Request))
- jQuery选择器总结
- 微信公众号开发之加密解密
- ARM 处理器
- kindeditor的使用
- HTML中的ID和NAME的区别[转]
- Linux 系统如何查看系统配置信息
- css布局
- springmvc之下载文件