POST 400 Bad Request The request sent by the client was syntactically incorrect
来源:互联网 发布:苏州基本办公软件 编辑:程序博客网 时间:2024/05/18 18:19
最近在做Web开发的时候,使用$.post提交数据,但是回调函数却没有被触发,按F12看控制台输出是:POST *** 400 Bad Request
后台是SpringMVC的,设置了断点也不会被触发。
后来查看JQuery资料了解到,$.post提交数据只有成功时才触发回调函数,于是改用$.ajax提交数据,添加error回调函数,得到错误信息了,如下图:
这个问题是什么原因造成的呢?
后来经过测试发现,是表单提交的内容数据类型与实体的(也就是数据表字段)的数据类型不匹配导致的。
在提交表单之前应该对用户输入的内容做验证,后台直接做映射了,没有做内容验证的机会。
2016-05-26更新
解决方法:
上面只是描述了问题产生的原因,而并没有给出解决的方法,很多小伙伴其实看到原因就已经想到解决方法了。
那有些小伙伴可能刚接触,还搞不清状况,我就再说一下解决的步骤:
1、改用$.ajax提交数据,添加error回调函数(其实在开发中严格来说是不允许使用$.post的,因为失去了错误处理的功能);
类似代码如下,具体请参考JQurey参考手册:
$.ajax({ type: "POST", url: "some.php", data: "name=John&location=Boston", success: function(msg){ alert( "Data Saved: " + msg ); }, error: function(XMLHttpRequest){ alert( "Error: " + XMLHttpRequest.responseText); }});这样你就可以通过 XMLHttpRequest.responseText 或XMLHttpRequest.responseHTML 了解到后端代码到底发生了什么错误。
2、不要把生成实体的工作交给MVC框架来完成
你的产生问题的代码,我猜测可能类似如下:
/** * 添加用户. **/@RequestMapping("/adduser.do")@ResponseBodypublic void adduser(HttpServletRequest request, HttpServletResponse response, User user) throws Exception {this.userManager.saveOrUpdate(user);}这样做就导致上面说过的问题:后台直接做映射了,没有做内容验证的机会;明明一个字段要求数字,结果用户输入了字母或汉字。
正确的方法类似如下:
/** * 添加用户. **/@RequestMapping("/adduser.do")@ResponseBodypublic void adduser(HttpServletRequest request, HttpServletResponse response) throws Exception {//将形参 user 拿到函数内部定义创建User user = new User();//这里对用户的年龄就要进行判断,具体规则你自己定义,我只是举个例子Integer age = getInteger("age");if (age != null){uset.SetAge(age);}this.userManager.saveOrUpdate(user);}protected Integer getInteger(String name) {String str = this.request.getParameter(name);if (StringUtils.isNotBlank(str)) { return Integer.valueOf(str);}return null; }
4 0
- POST 400 Bad Request The request sent by the client was syntactically incorrect
- HTTP 400: Bad Request The request sent by the client was syntactically incorrect
- http 400 Bad Request The request sent by the client was syntactically incorrect
- The request sent by the client was syntactically incorrect.
- The request sent by the client was syntactically incorrect.
- The request sent by the client was syntactically incorrect问题解决
- The request sent by the client was syntactically incorrect
- The request sent by the client was syntactically incorrect ()问题
- The request sent by the client was syntactically incorrect.
- The request sent by the client was syntactically incorrect
- The request sent by the client was syntactically incorrect.
- The request sent by the client was syntactically incorrect ()"解决办法
- The request sent by the client was syntactically incorrect
- The request sent by the client was syntactically incorrect ()解决方法
- The request sent by the client was syntactically incorrect.
- The request sent by the client was syntactically incorrect ()
- The request sent by the client was syntactically incorrect.
- The request sent by the client was syntactically incorrect解决办法
- 不容错过!开发者必备的十二大Android开发资源
- 3D MAX导出插件编写
- Extjs4.2+struts2+swfupload多文件上传
- Android wear环境搭建
- 老板和员工,到底谁是庄家
- POST 400 Bad Request The request sent by the client was syntactically incorrect
- HOJ 12825 Cup of Cowards(搜索)
- 【Python】 python线程指南
- ACPC 2013 J.Modified LCS
- 矩阵应用
- 关于android的AsyncTask。
- 摘录:Linux打Patch的方法
- SQL preparedstatement用法
- 华师大致学教育