用ajax提交数据添加失败

20170421w 2017-09-27 02:18:45
$("#user_save_btn").click(function(){
//alert($("#userAddModal form").serialize());
$.ajax({
url:"${APP_PATH}/user",
type:"POST",
date:$("#userAddModal form").serialize(),
success:function(result){
alert(result.msg);
}
});
});

<button type="button" class="btn btn-primary" id="user_save_btn">保存</button>

//tianjia
@RequestMapping(value="/user",method=RequestMethod.POST)
@ResponseBody
public Msg saveUser(User user){
System.out.println(1);
userService.saveUser(user);
return Msg.success();
}

点击提交后台提示
1
九月 27, 2017 2:06:19 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [dispacherServlet] in context with path [/SSM-Text] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException:
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
### The error may involve com.aisino.mapper.UserMapper.insertSelective-Inline
### The error occurred while setting parameters
### SQL: insert into user
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)

这是sql <insert id="insertSelective" parameterType="com.aisino.entity.User">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="password != null">
password,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="password != null">
#{password,jdbcType=VARCHAR},
</if>
</trim>
</insert> 这个是自动生成的mapper.xml文件的应该不会错。数据alert($("#userAddModal form").serialize());这个也会显示name和password 数据。
...全文
463 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39912309 2017-10-09
  • 打赏
  • 举报
回复
debug看下, userService.saveUser(user);user有没有参数,如果没有就是form表单里面参数名和实体类不一致
lianhy 2017-10-09
  • 打赏
  • 举报
回复
String 类型的最好再加上 xx!='' 的判断,在你的sqlmap里
lianhy 2017-10-09
  • 打赏
  • 举报
回复
date:$("#userAddModal form").serialize() vs data:$("#userAddModal form").serialize(),
wuyangzai1 2017-10-09
  • 打赏
  • 举报
回复
[quote=引用 4 楼 qq_39912309 的回复:] debug看下, userService.saveUser(user);user有没有参数,如果没有就是form表单里面参数名和实体类不一致[/quote
20170421w 2017-10-09
  • 打赏
  • 举报
回复
引用 2 楼 u012463264 的回复:
[quote=引用 1 楼 u012463264 的回复:] sql 语句有错误 ,是不是sql 少括号。
<insert id="addUser" parameterType="com.tjx.entity.User"> insert into User(id,username) values(#{id},#{username}) </insert> [/quote] 我用了你这种的。现在可以添加了。但是数据添加是空的。 前台的数据添加到后台怎么 获取的
20170421w 2017-10-09
  • 打赏
  • 举报
回复
引用 6 楼 Snowprincev 的回复:
date:$("#userAddModal form").serialize() vs data:$("#userAddModal form").serialize(),
data 这个改了一下就好了--!谢谢大神
别闹腰不好 2017-09-27
  • 打赏
  • 举报
回复
引用 1 楼 u012463264 的回复:
sql 语句有错误 ,是不是sql 少括号。
<insert id="addUser" parameterType="com.tjx.entity.User"> insert into User(id,username) values(#{id},#{username}) </insert>
别闹腰不好 2017-09-27
  • 打赏
  • 举报
回复
sql 语句有错误 ,是不是sql 少括号。

51,397

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧