关于springmvc用josn格式传参的时候出现的415错误

关于springmvc用josn格式传参的时候出现的415错误,已经一下解决方法。
首先这是我的代码:
1.js以及json的

<script type="text/javascript"  src="${pageContext.request.contextPath}/js/jquery.js"></script>
<script type="text/javascript">
   function check(){
	           var username=document.getElementById("username").value;
	           var password=document.getElementById("password").value;
	           var url="${pageContext.request.contextPath }/testjson";
	           var user= JSON.stringify ({username:username,password:password});
	           $.ajax({
	        	   url : url,
	        	   type : "POST",
	        	   dataType: 'json',
	        	   contentType: 'application/json',
	        	   data :user,
	        	   success : function(data){
	        		   if(data!=null){
	        			   alert("您输入的用户名为:"+data.username+
	        				      "您输入的密码为"+data.password	   
	        			   );
	        			   return true;
	        		   }else{
	        			   return false;
	        		   }
	        	   }
	           });
   }
</script>
<body >
<form>
  用户名:<input type="text"   name="username"  id="username">
  密码:<input  type="text"  name="password"  id="password"> 
<input  type="button" value="提交测试json交互"   onclick=" check() ">
</form>
</body>

2.后台controller的

    @RequestMapping(value = "/testjson")         
         @ResponseBody
         public User testjson(@RequestBody User user) {
                  System.out.println(user);
                  return user;
       }

注意的问题:
1.form表单中 type应该为button(稍微注意)

<script type="text/javascript"  src="${pageContext.request.contextPath}/js/jquery.js">

这个很重要,注意路径是相对路径,用json时必须引的东西

var url="${pageContext.request.contextPath }/testjson";

url路径也是相对路径,

4.我找了3 4 个小时的bug,
在这里插入图片描述
我的spring版本为4.1.6,我开始传数据的时候一直报415的错误,但是百度一直是说是 contentType: ‘application/json’, 的问题,后来我才发现,我后台使用pojo类接受的数据时报了415错误,原来是在这里插入图片描述
这三个包的版本不对,我换成了2.9.6的。
后台就ok了
在这里插入图片描述
ackson-core/2.9.6.jar的下载地址,其他的二个包可以跟着找。

https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.9.6/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值