拒绝设置不安全的标题" Cookie"浏览器中的错误但请求成功

时间:2015-01-31 18:54:24

标签: javascript angularjs

我正在使用Angularjs。当我使用Cookie设置xhr.setRequestHeader()标题时,我在Chrome上收到以下错误:

Refused to set unsafe header "Cookie"

但是,Cookie包含在请求中并成功发送到服务器。我似乎已正确配置所有内容以允许服务器和客户端上的Cookie标头:

对于服务器我有这些:

Header add Access-Control-Allow-Credentials "true"
客户端的

我指定了这些:

withCredentials

为什么会出现这个错误?

3 个答案:

答案 0 :(得分:8)

您从Chrome收到该错误,因为根据the XHR specificationsetRequestHeader方法不应使用forbidden header name设置标头。

根据规范:

  
    

禁止使用这些内容,以便用户代理完全控制它们。

  

相反,对于Angular 1.x,set the cookie使用$cookies,它将包含在后续xhr次请求中。

答案 1 :(得分:0)

这个"模板"适合我

<小时/> 打开开发者工具并尝试运行$.ajaxsetRequestHeader放入beforeSend方法。

<?php
  header('Content-Type: text/html; charset=UTF-8');
  header('Access-Control-Allow-Origin: *');
  header('Access-Control-Allow-Headers: *');
  header('Access-Control-Expose-Headers: *');
  header('Access-Control-Allow-Credentials: true');
?><!doctype html>
<html lang="en-US">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  </head>
  <body>
    <script defer="defer" src="https://code.jquery.com/jquery-git.js"></script>
  </body>
</html>

取自 Liberated HTML5 W3C's Cross-Origin Resource Sharing

答案 2 :(得分:-1)

如果您发送了cookie,我认为这样就足够了。拥有“crossDomain:true,         withCredentials:true “解决了”拒绝设置不安全标题“Cookie的问题”我也遇到过。“即使警报信息仍然存在,但是我发送了cookie并且回复正确。

相关问题