JQuery调用Spring Rest服务:Cross-Origin问题与ParseError

时间:2015-02-26 16:55:43

标签: java jquery spring rest spring-mvc

我试图用jquery调用REST服务,但是我得到一个Cross Origin问题(当我没有在jquery调用中指定数据类型时)或者一个parseerror(当我这样做时)。

JQuery使用:

function requestData() {
    $.ajax({
        url: 'http://xxx.xx.xx.xxx:8080/Project/api/',
        type: 'GET',
        dataType:"jsonp",
        success: function(json) { 
            console.log("OK");
            console.log(json);
        },
        error: function(xhr, status, error) {
          console.log("NO");
          console.log("STATUS:" + status);
        }
    });
}

服务器端,我这样使用Spring REST:

@RequestMapping(value = "/", method = RequestMethod.GET)
public @ResponseBody List<Entity> getEntities() {
    List<Entity> found = controller.findEntities();

    return found;
}

序列化数据是正确的,因为在浏览器中调用REST API会产生正确的JSON。

我还添加了以下CORS过滤器,似乎没有被执行,因为&#34; AA&#34;没有打印:

@Component
public class SimpleCORSFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {

        System.out.println("AA");

        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods",
                "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        chain.doFilter(req, res);
    }

    public void init(FilterConfig filterConfig) {}
    public void destroy() { }
}

我的web.xml:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

<servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>simpleCORSFilter</filter-name>
        <filter-class>org.lh.xxx.web.SimpleCORSFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>simpleCORSFilter</filter-name>
        <servlet-name>/*</servlet-name>
    </filter-mapping>
    <filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

它有什么问题?

1 个答案:

答案 0 :(得分:0)

问题严重与过滤器未执行有关:Spring Filter not getting invoked

执行过滤器时,将json更正返回给调用客户端。