本文主要是介绍使用SpringMVC 传递JSON字符串返回415问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
今天在springMVC传递Json的时候,浏览器端返回一直为415
总结以下可能遇到的问题:
1.没有导入jar依赖包
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.9.3</version>
</dependency><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.3</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.9.3</version>
</dependency>
2.<!-- 使用全扫描 -->
<mvc:annotation-driven/>
不要自己定义解析器,否则还要单独配置
3.
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(function(){alert();var params = '{"id": 1,"name": "测试商品","price": 99.9,"detail": "测试商品描述","pic": "123456.jpg"}';jqXHR = $.ajax({url : "${pageContext.request.contextPath }/json.action",data : params,contentType : "application/json;charset=UTF-8",//发送数据的格式type : "post",dataType : "json",//回调});jqXHR.done(function(data){alert(data.name);});
});
传递数据以定加
contentType : “application/json;charset=UTF-8”,//发送数据的格式
不然SpringMVC不认识
这篇关于使用SpringMVC 传递JSON字符串返回415问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!