简体   繁体   中英

Consuming a RESTful Web Service with jQuery

about the guide at:

https://spring.io/guides/gs/consuming-rest-jquery/

I'm getting:

XMLHttpRequest cannot load h__p://rest-service.guides.spring.io/greeting.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'h__p://localhost:8000' is therefore not allowed access.

Sorry for the noob question, but what would I need to to in order to test the code on the abovementioned page?

Thanks everyone!

That seems to be a cors issue. Meaning you are serving the app not from spring but from elseweare. Youll have to create a filter to be able to configure the header

@Component
public class CORSFilter implements Filter{

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {

        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);

    }

    @Override
    public void destroy() {

    }

}

That should fix it

This looks like a CORS issue. I have faced this before with my spring mvc application. I have a @component CORS implementation that solves it. You might need to configure your front end to work with CORS too.

Also keep in mind that for security issues allowing any origins to access your API might not be safe. So for production implementations ("Access-Control-Allow-Origin", "*") is not recommended.

Original source: http://patrickgrimard.com/2013/12/12/cross-origin-resource-sharing-cors-requests-with-spring-mvc/

    import java.io.IOException;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.stereotype.Component;

    @Component
    public class SimpleCORSFilter implements Filter {

        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) res;
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
            response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
            response.addHeader("Access-Control-Allow-Headers", "Content-Type");
            chain.doFilter(req, res);
        }

        public void init(FilterConfig filterConfig) {}

        public void destroy() {}

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM