【问题标题】:406 (Inacceptable) error with ajax and spring mvc not mavenajax和spring mvc不是maven的406(不可接受)错误
【发布时间】: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


    【解决方案1】:

    您的 ajax 调用在数据元素中有错误:

    $.ajax({ 
        type: "POST", 
        url: "car.html", 
        data: {
            test: "SomeOtherValueThanDataBecauseOtherwiseItsConfusing"
        },
        dataType: 'json',
        contentType: "application/json",
        success: function (data) { 
           alert(data);
           console.log(data);
        }
    });
    

    【讨论】:

      【解决方案2】:

      将 MediaType 添加到控制器方法 RequestMapping

      @RequestMapping(value = "/car", method = RequestMethod.POST,  headers = {"content-type=application/json"})
      

      【讨论】:

        【解决方案3】:

        试试

        url: "/car", 
        data: "taiKhoan: data",
        

        "=" 运算符在此处的 jquery 中无效,并且您的 url 可能不匹配。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-04-26
          • 1970-01-01
          • 2013-03-18
          • 2011-11-20
          • 1970-01-01
          • 2019-04-30
          相关资源
          最近更新 更多