【问题标题】:Send a Cookie in a RestAngular request在 RestAngular 请求中发送 Cookie
【发布时间】:2017-01-27 01:11:01
【问题描述】:

我有一个 Restangular.getAll 函数,

当我调用它时,cookie 不包含在 API 请求中,这与获得的 HTML 请求不同。

如果我强制执行:

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

错误是:

Refused to set unsafe header "Cookie"

如果我添加到 app.config:

RestangularProvider.setDefaultHttpFields({
    withCredentials: true
});

错误是:

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.

请注意hostlink是page link和host link的缩写。

编辑: 我在春天的 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() {
    }

}

【问题讨论】:

标签: javascript angularjs spring cookies restangular


【解决方案1】:

您的服务器必须为 Access-Control-Allow-Origin 返回特定内容: 您可以在此处提供您的 webapp 主机 URL,或动态复制源信息(用于开发目的)。

注意:它是服务器端修复

【讨论】:

  • 似乎其他页面您没有使用 withCredentials:true
猜你喜欢
  • 1970-01-01
  • 2012-10-13
  • 2013-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 2023-03-25
相关资源
最近更新 更多