【发布时间】:2016-03-08 02:41:44
【问题描述】:
我正在尝试使用 spring MVC 运行 ajax 方法,但我收到错误 406: "406 (Inacceptable)"
带有磁贴的 Spring MVC,动态 Web 项目
帮我配置 servlet.xml
控制器:
@RestController
public class JsonController {
@RequestMapping(value = "/car", method = RequestMethod.POST,headers= "application/json")
public @ResponseBody Car load(@ModelAttribute(value="test") String test){
Car car = new Car();
car.setColor("Blue");
car.setMiles(100);
car.setVIN("1234");
return car;
}
}
查看:
<script src="http://code.jquery.com/jquery-2.1.4.js"></script>
<script>
$(document).ready(function () {
$.ajax({
type: "POST",
url: "car.html",
data: {
test: "datatest"
},
dataType: 'json',
contentType: "application/json",
success: function (data) {
alert(data);
console.log(data);
}
});
});
</script>
小服务程序:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<context:component-scan base-package="controller" />
<mvc:annotation-driven />
<context:annotation-config />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.tiles3.TilesView</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
</beans>
请问大家有什么解决办法吗?
谢谢大家!
【问题讨论】:
标签: json spring spring-mvc model-view-controller