2016-09-19 112 views
0

我有一个Restangular.getAll功能,发送一个Cookie在RestAngular请求

当我把它叫做Cookie不包含在API请求,不像HTML请求谁GOTS。

如果我强迫一个:

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. 

请注意主机链接是页面链接和主机链接的缩写。

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

} 
+0

尝试:http://stackoverflow.com/questions/29696406/restangular-how-to-read-cookie-and-add-to-headers-at-each-查询 –

回答

0

服务器已返回特定内容访问控制允许来源: 你可以有给你的web应用主机URL,或动态复制原始信息(用于开发目的)。

注意:它是一个服务器端修复

+0

它似乎与您没有使用withCredentials的其他页面:true –