springmvc 使用jq传递json数据时出现415错误
出现415错误是因为解析json时出现了错误,通过排查几点就能解决。
样例:
<script>
function requestByJson() { var datatest = {"id":0,"username":"kinome","password":"pwd"}; $.ajax({ type : 'post', url : '${pageContext.request.contextPath}/updateUserInfo.action', dataType:"json", //设置contentType类型为json contentType : 'application/json;charset=utf-8', //json数据 data : JSON.stringify(datatest), //请求成功后的回调函数 success : function(data) { alert(data.id+" "+data.username+" "+data.password); } }); }
</script>
必须设置:
dataType:"json"
contentType : 'application/json;charset=utf-8'
data : JSON.stringify(存放json的变量) // 这里存在一个对象的问题,所以需要封装起来使用
controller方面:

必须配置: consumes = "application/json" 用来接收json数据
(按照上图的编写方式就能正常接收并返回json数据)
spring所有依赖版本: 4.1.7.RELEASE
spring-servlet.xml 必须配置 <mvc:annotation-driven /> 用来进行自动注册并加载jackson相关文件
pom.xml必须配置jackson相关依赖,且 (重点)spring版本和jackson版本如果不兼容也会导致无法解析json,进而提示415错误
<!--jackson相关依赖 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.4</version>
</dependency>
成功返回json数据并通过jq的回调函数执行:

springmvc 使用jq传递json数据时出现415错误的更多相关文章
- Spring MVC中传递json数据时显示415错误解决方法
在ajax中设置 ContentType为'application/json;charset=utf-8' 传递的data类型必须是json字符串类型:{“key”:"value" ...
- MVC中利用ViewBag传递Json数据时的前端处理方法
用viewBag传递Json字符串到前端时,json字符串中的“会被转义为& quot,前端处理方法为@Html.Raw(Json.Encode(ViewBag.Data)),再用eval() ...
- SpringMVC之Controller传递JSON数据到页面
在Controller中,组装好JSON格式的数据,然后输入到页面,或者通过ajax请求在页面进行解析,都可以做到. 1.Controller /** * JSON DATA TO PAGE VEIW ...
- SpringMVC 配置.html拦截时,返回JSON数据时出现406错误解决方案
[说明]在SpringMVC框架的使用中常常会使用@ResponseBody注解,修饰"处理器"(Controller的方法),这样在处理器在返回完毕后,就不走逻辑视图,而是将返回 ...
- springmvc ajax传递json值时出现400错误 (问题出在sql上)
问题出在sql的int不能达到 10000000000 这个值,从 int(11) 修改成 varchar(20) 就可以正常存储了
- SpringMVC传递JSON数据
文章目录 一.前后端传递和接收JSON数据 1:是要Ajax默认格式来传递数据(*) 2:使用application/json格式来传递数据 二.spring-web.xml中需要如下配置 一.前后端 ...
- SpringMVC返回JSON数据时日期格式化问题
https://dannywei.iteye.com/blog/2022929 SpringMVC返回JSON数据时日期格式化问题 博客分类: Spring 在运用SpringMVC框架开发时,可 ...
- Ajax传递json数据简介和一个需要注意的小问题
Ajax传递json数据 Ajax操作与json数据格式在实际中的运用十分广泛,本文为大家介绍一个两者相结合的小案例: 项目结构 我们新建一个Django项目,在里面创建一个名为app01的应用: p ...
- JavaScript onclick传递对象参数(easyui传递一行数据时)错误:uncaught SyntaxError: Unexpected identifier
JavaScript onclick传递对象参数(easyui传递一行数据时)错误:uncaught SyntaxError: Unexpected identifier 博主遇到的是用onclick ...
随机推荐
- mysql 创建数据数据库 (避免新建的库名已经存在、设置编码)
1.创建数据库的 create database 数据库名 eg: Create database mydatabase 查看已创建的数据: show databases; 结果: 2.数据库名所对应 ...
- Ubuntn系统(虚拟机)忘记密码的解决方法
1.重启ubuntu系统,开机时长按shift按键进入GRUB菜单,选择第二个高级选项,enter键进入.如下图: 2.在高级选择中选择Recovery mode模式,键盘按“e”键进入编辑模式.如下 ...
- November 22nd 2016 Week 48th Tuesday
Eventually, you will learn to cry on the inside. 终有一天,你得学会让眼泪在心里流. Cry on the inside. I am tired, an ...
- CI(2.2) 配置 jquery的上传插件Uploadify(v3.2) 上传文件
1.下载uploadify, 我的是v3.2 2.模板页面引入: <base href='{base_url()}' /> <script type="text/jav ...
- Jmeter入门3 http请求—content-type与参数
本文讲三种content-type以及在Jmeter中对应的参数输入方式 第一部分:目前工作中涉及到的content-type 有三种: content-type:在Request Headers里, ...
- DNS Brand
1) You must add glue records (child nameservers) to your-domain.com from your domain's registrar con ...
- Tomcat6的相关配置
1. Tomcat无安装部署: 本文windows用的是win7,ubuntu用的是12.04 LTS,tomcat版本是1.6 1.1. windows上的tomcat无安装部署 1.1.1. 确认 ...
- Spring常用jar包的功能
jar名称 描述 spring-framework.jar spring框架比较完整的功能,core+aop+ioc+transaction spring-core.jar 基本上的核心工具类,一些u ...
- DIV+CSS:如何编写代码才能更有效率
如何编写CSS代码才能更有效率?这是许多网页制作者与开发者都关心的问题.大概没有什么魔法,可以保证一下就把你的样式表缩小到百分之多少,但合理的 CSS 编码与组织技巧,的确能够帮助你的更有效率地写出更 ...
- haproxy 启动错误
在haproxy启动时会报错 ALERT] / () : Starting proxy short_message: cannot bind socket 问题1,如果bind的是vip,则需要内核添 ...