简体   繁体   English

在RestAngular请求中发送Cookie

[英]Send a Cookie in a RestAngular request

I got a Restangular.getAll function, 我有一个Restangular.getAll函数,

when i call it the cookies are not included in the API request, unlike the HTML requests who gots. 当我将其称为cookie时,API请求中不包含cookie,这与获得HTML请求的人不同。

if I force a: 如果我强迫:

Restangular.setDefaultHeaders({ Cookie: function() { return "foo " + $cookies.get('foo'); } })

The error is: 错误是:

Refused to set unsafe header "Cookie"

If I add to app.config: 如果我添加到app.config中:

RestangularProvider.setDefaultHttpFields({
    withCredentials: true
});

The error is: 错误是:

XMLHttpRequest cannot load [*link*] A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true.
 Origin [*host*] is therefore not allowed access. 
The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute.

Please note that host and link are the abbreviations for page link and host link. 请注意, 主机链接是页面链接和主机链接的缩写。

EDIT: My CORSFilter in Spring: 编辑:我在春季的CORSFilter:

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CORSFilter implements Filter {

    public CORSFilter() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        HttpServletRequest request = (HttpServletRequest) req;
        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, authorization, content-type");

        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            chain.doFilter(req, res);
        }
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void destroy() {
    }

}

Your server has to return a particular content for Access-Control-Allow-Origin: You could give your webapp host URL there, or dynamically copy the origin information (for development purposes). 您的服务器必须返回Access-Control-Allow-Origin的特定内容:您可以在其中提供webapp主机URL,或动态复制原始信息(出于开发目的)。

NOTE: its a server side fix 注意:其服务器端修复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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