ajax结合springmvc报406错误

在springmvc中使用ajax发现报了406错误,查了一下:
如下

the resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request “accept” headers

大概意思就是说响应头不一样,也是springmvc返回的html数据,然后我们网页端接收的是Application/json,所以需要springmvc将返回的数据进行json化处理

而且我的controller是能够获取到值的,更加说明了返回的时候出现问题

所以我们需要引入相关包,让springmvc自动处理即可
如果使用的是maven

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.4.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.1.1</version>
</dependency>

如果上面还不行,请再加一个jackson-annotions的包

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.4.1.1</version>
</dependency>

方法二:
可以手动添加jackson的包即可

其实我觉得只要是个能够解析json的包应该都可以,包括不仅限于这几个。未测试

相关文章

开发过程中是不可避免地会出现各种异常情况的,例如网络连接...
说明:使用注解方式实现AOP切面。 什么是AOP? 面向切面编程...
Spring MVC中的拦截器是一种可以在请求处理过程中对请求进行...
在 JavaWeb 中,共享域指的是在 Servlet 中存储数据,以便在...
文件上传 说明: 使用maven构建web工程。 使用Thymeleaf技术...
创建初始化类,替换web.xml 在Servlet3.0环境中,Web容器(To...