Spring MVC Ajax错误406

时间:2016-01-14 14:55:29

标签: java spring spring-mvc

我遇到简单的ajax POST请求问题。当我在页面加载时调用doAjax时,它返回我的erorr 406是不可接受的。我有正确的依赖。

简单模型

package proj1.models;

public class Popravilo{
    public String id = null;
    public String name = null;
}

控制器

@RequestMapping(value = "car_list", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody Popravilo showPopravila(){
        Popravilo p = new Popravilo();
        p.id="1";
        p.name ="p1";

        return p;
}

的Ajax

 $(document).ready(function(){
        doAjaxPost();
    });
    function doAjaxPost() {
        $.ajax({
            type: "POST",
            url: "car_list.htm",
            success: function(response) {
               console.log(response);
            }
        });
    }

Dispatcher servlet

<?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: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.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">


 <mvc:resources mapping="WEB-INF/resources/*" location="resources/"     cache-period="31556926"/>

    <mvc:annotation-driven />

<context:component-scan base-package="proj1.controllers"></context:component-scan>

</beans>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>proj1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
      <servlet-name>dispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup> 
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.html</url-pattern>
    <url-pattern>*.htm</url-pattern>
    <url-pattern>*.json</url-pattern>
    <url-pattern>*.xml</url-pattern>
  </servlet-mapping>

</web-app>

错误信息 Headers

2 个答案:

答案 0 :(得分:0)

406 Not Acceptable意味着此请求的资源仅能够根据请求中发送的接受标头生成具有不可接受的内容特征的响应实体。因此,您需要将Content-Type指定为application/json,这是您从服务器发送的内容。

答案 1 :(得分:0)

HTTP状态406表示您的客户端无法处理服务器返回的数据。

尝试更改ajax调用,如下所示

$.ajax({
    type: "POST",
    url: "car_list.htm",
    dataType: "json",/*changed the Accept header, acceptable datatypes*/
    success: function(response) {
       console.log(response);
    }
});