转:POST 400 Bad Request The request sent by the client was syntactically incorrect
最近在做Web开发的时候,使用$.post提交数据,但是回调函数却没有被触发,按F12看控制台输出是:POST *** 400 Bad Request
后台是SpringMVC的,设置了断点也不会被触发。
后来查看JQuery资料了解到,$.post提交数据只有成功时才触发回调函数,于是改用$.ajax提交数据,添加error回调函数,得到错误信息了,如下图:
这个问题是什么原因造成的呢?
后来经过测试发现,是表单提交的内容数据类型与实体的(也就是数据表字段)的数据类型不匹配导致的。
在提交表单之前应该对用户输入的内容做验证,后台直接做映射了,没有做内容验证的机会。
解决方法:
上面只是描述了问题产生的原因,而并没有给出解决的方法,很多小伙伴其实看到原因就已经想到解决方法了。
那有些小伙伴可能刚接触,还搞不清状况,我就再说一下解决的步骤:
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")
@ResponseBody
public void adduser(HttpServletRequest request, HttpServletResponse response, User user) throws Exception {
this.userManager.saveOrUpdate(user);
}
这样做就导致上面说过的问题:后台直接做映射了,没有做内容验证的机会;明明一个字段要求数字,结果用户输入了字母或汉字。
正确的方法类似如下:
/**
* 添加用户.
**/
@RequestMapping("/adduser.do")
@ResponseBody
public 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;
}
转:POST 400 Bad Request The request sent by the client was syntactically incorrect的更多相关文章
- HTTP Status 400 - description The request sent by the client was syntactically incorrect.
HTTP Status 400 - type Status report message description The request sent by the client was syntacti ...
- 错误:The request sent by the client was syntactically incorrect的解决
问题: 错误400-The request sent by the client was syntactically incorrect. springMVC中,某个页面提交时报400错误,如下图. ...
- SpringMVC---400错误The request sent by the client was syntactically incorrect ()
在SpringMVC中使用@RequestBody和@ModelAttribute注解时遇到了很多问题,现记录下来. @ModelAttribute这个注解主要是将客户端请求的参数绑定参数到一个对象上 ...
- The request sent by the client was syntactically incorrect.
HTTP Status 400 - type Status report message description The request sent by the client was syntacti ...
- SpringMVC报错The request sent by the client was syntactically incorrect ()
springmvc数据绑定出的错 在数据绑定的时候一定要主意Controller方法中的参数名和jsp页面里的参数名字是否一致或者按照绑定的规范来写, 如果不一致,可能回报如下错误: The requ ...
- "The request sent by the client was syntactically incorrect ()"问题定位及解决:
Spring MVC "The request sent by the client was syntactically incorrect ()"解决办法: 把spring日志级 ...
- The request sent by the client was syntactically incorrect问题解决
The request sent by the client was syntactically incorrect意思嘛,google翻译一下 通过日志不难看出,是由参数不匹配造成的. 所以对于Da ...
- spring mvc 在上传图片时,浏览器报The request sent by the client was syntactically incorrect
项目中,在一个jsp页面里其它图片上传是功能是可以使用的,当我自己新加了一个图片上传时,提交表单后,浏览器报The request sent by the client was syntactical ...
- 又见The request sent by the client was syntactically incorrect ()
前几天遇到过这个问题(Ref:http://www.cnblogs.com/xiandedanteng/p/4168609.html),问题在页面的组件name和和注解的@param名匹配不对,这个好 ...
随机推荐
- asp.net mvc3.0安装失败之终极解决方案
安装失败截图 原因分析 因为vs10先安装了sp1补丁,然后安装的mvc3.0,某些文件被sp1补丁更改,导致“VS10-KB2483190-x86.exe”安装不了,造成安装失败. 解决方案 方法1 ...
- tftp命令详解
TFTP协议简介TFTP是用来下载远程文件的最简单网络协议,它其于UDP协议而实现.嵌入式linux的tftp开发环境包括两个方面: 一是linux服务器端的tftp-server支持,二是嵌入式目标 ...
- 【Arcgis for android】程序运行出错原因分析及解决(超详细)
查看项目下是否有libs文件夹,正常情况下其中应该有 如果没有,在项目上右键 ->arcgis tools->convert to arcgis android project 排除了上述 ...
- JavaScript CheckBox实现全选和部分选择
<html> <head> <script> function BatchAddToBasket() { var questionNums = ''; var ch ...
- oracle 多表连接查询 join(一)
一.简介: 多表连接查询通过表之间的关联字段,一次查询多表数据. 下面将依次介绍 多表连接中的如下方法: 1.from a,b 2.inner join 3.left outer join 4.rig ...
- P1080 国王游戏
题意: 让n 位大臣排成一排,国王站在队伍的最前面. 排好队后,所有的大臣都会获得国王奖赏的若干金币, 每位大臣获得的金币数分别是:排在该大臣前面的所有人的左手上的数的乘积除以他自己右手上的数,然后向 ...
- 「NOI.AC」Leaves 线段树合并
题目描述 现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有\(n\)个叶子节点,满足这些权值为\(1\dots n\)的一个排列).可以任意交换每个非叶子节点的左右孩子. ...
- Drop user 报ORA-00600 [KTSSDRP1]
一客户删除一个数据库用户THH时报错: 说明在获取seg$时没有找到相应的条目,先来解释下这个600错误的参数含义: Arg [a] Tablespace number Arg [b] File nu ...
- spring 事务 配置 多个
Spring中事务控制相关配置: <bean id="txManager" class="org.springframework.jdbc.datasource.D ...
- ssh-keygen生成公私钥免密码登录远程服务器
1.终端输入命令:ssh-keygen -t rsa ssh-keygen命令专门是用来生成密钥的.该命令有很多选项,这里列出了最基本的四个: -t 用来指定密钥类型(dsa | ecdsa | ed ...