最近在做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的更多相关文章

  1. 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 ...

  2. 错误:The request sent by the client was syntactically incorrect的解决

    问题: 错误400-The request sent by the client was syntactically incorrect. springMVC中,某个页面提交时报400错误,如下图. ...

  3. SpringMVC---400错误The request sent by the client was syntactically incorrect ()

    在SpringMVC中使用@RequestBody和@ModelAttribute注解时遇到了很多问题,现记录下来. @ModelAttribute这个注解主要是将客户端请求的参数绑定参数到一个对象上 ...

  4. 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 ...

  5. SpringMVC报错The request sent by the client was syntactically incorrect ()

    springmvc数据绑定出的错 在数据绑定的时候一定要主意Controller方法中的参数名和jsp页面里的参数名字是否一致或者按照绑定的规范来写, 如果不一致,可能回报如下错误: The requ ...

  6. "The request sent by the client was syntactically incorrect ()"问题定位及解决:

    Spring MVC "The request sent by the client was syntactically incorrect ()"解决办法: 把spring日志级 ...

  7. The request sent by the client was syntactically incorrect问题解决

    The request sent by the client was syntactically incorrect意思嘛,google翻译一下 通过日志不难看出,是由参数不匹配造成的. 所以对于Da ...

  8. spring mvc 在上传图片时,浏览器报The request sent by the client was syntactically incorrect

    项目中,在一个jsp页面里其它图片上传是功能是可以使用的,当我自己新加了一个图片上传时,提交表单后,浏览器报The request sent by the client was syntactical ...

  9. 又见The request sent by the client was syntactically incorrect ()

    前几天遇到过这个问题(Ref:http://www.cnblogs.com/xiandedanteng/p/4168609.html),问题在页面的组件name和和注解的@param名匹配不对,这个好 ...

随机推荐

  1. Linux系统命令Top/free的使用及参数详解

    1.作用 top命令用来显示执行中的程序进程,使用权限是所有用户. 2.格式 top [-] [d delay] [q] [c] [S] [s] [i] [n] 3.主要参数 d:指定更新的间隔,以秒 ...

  2. 《Maven实战》笔记-3-Maven仓库

    一.Maven仓库的分类 1.本地仓库 一般来说,在Maven项目目录下,没有诸如lib/这样用来存放依赖文件的目录. 要自定义本地仓库目录地址时,可以编辑文件~/.m2/setting.xml,设置 ...

  3. P与NP问题详解

    P,NP,NPC问题,这或许是众多OIer最大的误区之一. 本文就为大家详细讲解如上三个问题. 前序: 你会经常看到网上出现“这怎么做,这不是NP问题吗”.“这个只有搜了,这已经被证明是NP问题了”之 ...

  4. CHSaveData

    NSData数据 NSStream文件流 NSCache缓存 SQLite NSFileManager文件管理 NSUserDefaults数据存储 PList数据存储 NSKeyedArchiver ...

  5. SAP 740GUI客户端快捷方式取消密码登陆

    客户端电脑:WIN10 专业版64位. 1. 进入注册表编辑器(regedit) .进入如下路径:HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\SAP\SAPLogo ...

  6. 《图解HTTP》阅读笔记--第六章--HTTP首部

    第六章.HTTP首部 <非常重要且恐怖的一章了> HTTP报文=报文首部+(CR+LF)+报文实体 首部字段:HTTP报文首部字段=(首部字段名:字段值)们---类型*4: 通用首部字段( ...

  7. Halum UVA - 11478 差分约束

    输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 复制 2 1 1 2 10 2 1 1 2 -10 3 3 1 2 4 2 3 2 3 1 5 4 5 2 3 4 4 2 5 3 ...

  8. vue.js路由嵌套

    <!DOCTYPE html> <html> <head> <title></title> <meta charset="u ...

  9. 各平台安装和使用Docker的差异

    在Mac上运行Docker 使用Docker for Mac 系统要求 开启虚拟化的硬件支持,可以通过命令来检查:sysctl kern.hv_support OS X 10.10.3 或者更高版本 ...

  10. java中list里面存放map,根据map中的某一个字段进行排序

    package com; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; ...