springmvc 神奇的post方式提交到控制层,参数却是空值。

day_moon 2017-09-23 11:45:20
代码如下:
<script type="text/javascript">
function toLogin() {
$.ajax({
type:'POST',
url:'${pageContext.request.contextPath}/page/login.do',
contentType:'application/json;charset=UTF-8', //内容类型
data: {
name: $('#uname').val(),
pwd: $('#upwd').val()},
success:function(data) {
alert(data);
console.log(data)
}, error:function(data){
console.log('错误')
}
});
}
</script>
下面是空制层:
@Resource
private UserService userService;
@ResponseBody
@RequestMapping(value = "/login.do", method = RequestMethod.POST)
public String start(User user) {
String name=user.getName();
System.out.println("userName is:"+user.getName()+"密码是"+user.getPwd());

int userInt=this.userService.findByName(name);
if(userInt>0){
return "登录成功";
}else{
return "登录失败";
}

}
可以获取到页面输入的值,提交到后台确是空值。后面我就把post改为get方法,是有值的。
1、问题,我现在想用post方式来提交数据,不知道怎么改。
...全文
2017 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
梦天城 2017-09-30
  • 打赏
  • 举报
回复
引用 12 楼 a98985 的回复:
我想问一下这个Mye2017有破解版没.
这年头不都破解的么
qq_39912309 2017-09-29
  • 打赏
  • 举报
回复
你看下User实体类的属性名和你页面上的参数名一致吗?
a98985 2017-09-29
  • 打赏
  • 举报
回复
我想问一下这个Mye2017有破解版没.
god de gold 2017-09-27
  • 打赏
  • 举报
回复
User前面加个@requestBody, 也可以在requestMapping中加consumes = "application/json;charset=utf-8"
aa6501342 2017-09-27
  • 打赏
  • 举报
回复
引用 8 楼 xwn_2016 的回复:
这个contentType换成"application/x-www-form-urlencoded;charset=utf-8", 增加 dataType:"json", 后台方法中增加 HttpSevletRequest request参数
增加 dataType:"json", 正解
xwn_2016 2017-09-26
  • 打赏
  • 举报
回复
这个contentType换成"application/x-www-form-urlencoded;charset=utf-8", 增加 dataType:"json", 后台方法中增加 HttpSevletRequest request参数
枫舞蝶刺 2017-09-26
  • 打赏
  • 举报
回复
试下 @ModelAttribute ( "user" ) User user
day_moon 2017-09-25
  • 打赏
  • 举报
回复
都试过了还是报空值 跪求大神们顶起。。。。。。。
java_yinxuan 2017-09-25
  • 打赏
  • 举报
回复
前台ajax请求时,参数怎么都不加引号 不应该是这样吗 data: { "name": $('#uname').val(), "pwd": $('#upwd').val()},
Freefish1994 2017-09-25
  • 打赏
  • 举报
回复
User实体类加上@RequestBody注解试试
lifewell1 2017-09-25
  • 打赏
  • 举报
回复
$.post('${pageContext.request.contextPath}/page/login.do',{name: $('#uname').val(), pwd: $('#upwd').val()},function(data){ });
lifewell1 2017-09-25
  • 打赏
  • 举报
回复
换 ‘user.name’ 试试
yannsPeng 2017-09-25
  • 打赏
  • 举报
回复
@RequestMapping(value = "/login.do", method = RequestMethod.POST)
public  String start(User user,@RequestParam("name")String name,@RequestParam("pwd")String pwd) {
String name=user.getName();
System.out.println("userName is:"+user.getName()+"密码是"+user.getPwd());

int  userInt=this.userService.findByName(name);
if(userInt>0){
return "登录成功";
}else{
return "登录失败";
}
这样获取参数试试。看看前台请求报文有没有参数传到后台呢。

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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