【问题标题】:Angularjs / sailsjs :Access-Control-Allow-Origin is not allowed by Access-Control-Allow-HeadersAngularjs/sailsjs:Access-Control-Allow-Headers 不允许访问控制允许来源
【发布时间】:2015-06-18 05:22:42
【问题描述】:

我正在努力解决 angular js 和sails.js(node.js 框架)之间的 cors 问题

我尝试修复错误:XMLHttpRequest 无法加载http://localhost:1337/en/auth/forgetpass/email。 Access-Control-Allow-Headers 不允许请求标头字段 Access-Control-Allow-Origin。

当我不激活我的拦截器时,它运行良好。我没有这个错误。当我激活它时,我有错误。

在我的 .config 中,我设置了以下代码:

  //Enable cross domain calls
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];

$httpProvider.defaults.headers.common['Access-Control-Allow-Headers'] = '*';
$httpProvider.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
$httpProvider.defaults.headers.common['Access-Control-Allow-Methods'] = 'GET,POST,PUT,HEAD,DELETE,OPTIONS';

$httpProvider.interceptors.push('TokenInterceptor');

然后在我的拦截器中设置以下代码:

    return {
    request: function (config) {
      var id = Session.getprop('id');
      if(id) {
        config.headers = config.headers || {};
        config.headers.Authorization = 'Bearer ' + id;
      }
        return config;
    }, ...

最后,chrome network tab的结果是:

Remote Address:127.0.0.1:1337
Request URL:http://localhost:1337/en/auth/forgetpass/email
Request Method:OPTIONS
Status Code:200 OK

Response Headers
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:*
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS, HEAD
Access-Control-Allow-Origin:*
Allow:GET,POST,PUT,HEAD,DELETE,TRACE,COPY,LOCK,MKCOL,MOVE,PROPFIND,PROPPATCH,UNLOCK,REPORT,MKACTIVITY,CHECKOUT,MERGE,M-SEARCH,NOTIFY,SUBSCRIBE,UNSUBSCRIBE,PATCH
Connection:keep-alive
Content-Length:154
Content-Type:text/html; charset=utf-8
Date:Sun, 12 Apr 2015 23:51:14 GMT
Set-Cookie:sails.sid=s%3A-bZxQgFntbDqTtaFyWDFFgFr.szR0F68VfIBjVW9kyans9d6v5fz7RMtalQCoMFdbH%2Fg; Path=/;   HttpOnly
X-Powered-By:Sails <sailsjs.org>

Request Headers
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:access-control-allow-origin, accept, access-control-allow-headers, access-control-allow-methods
Access-Control-Request-Method:POST
Connection:keep-alive
Host:localhost:1337
Origin:http://localhost:9000
Referer:http://localhost:9000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36

我仍然遇到同样的错误。一个想法?

非常感谢!

【问题讨论】:

  • 什么是“拦截器”?这不是sails.js 的概念。
  • 拦截器是一个angularjs概念

标签: angularjs sails.js


【解决方案1】:

好的,我终于找到了问题。

我将响应和请求标头与拦截器和它进行了比较。

我将我的代码更改如下,它可以工作。

在 angularjs 的 app.js 中

我评论了所有的标题部分。

   //Enable cross domain calls
  /*  $httpProvider.defaults.useXDomain = true;

//Remove the header used to identify ajax call  that would prevent CORS from working
delete $httpProvider.defaults.headers.common['X-Requested-With'];

$httpProvider.defaults.headers.common['Access-Control-Allow-Headers'] = 'origin, content-type, accept';
$httpProvider.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
$httpProvider.defaults.headers.common['Access-Control-Allow-Methods'] = 'GET,POST,PUT,HEAD,DELETE,OPTIONS';*/

$httpProvider.interceptors.push('TokenInterceptor');

在我的sails.js cors 设置配置中,我注释了方法和标题。而且效果很好。

    module.exports.cors = {

  /***************************************************************************
  *                                                                          *
  * Allow CORS on all routes by default? If not, you must enable CORS on a   *
  * per-route basis by either adding a "cors" configuration object to the    *
  * route config, or setting "cors:true" in the route config to use the      *
  * default settings below.                                                  *
  *                                                                          *
  ***************************************************************************/

   allRoutes: true,

  /***************************************************************************
  *                                                                          *
  * Which domains which are allowed CORS access? This can be a               *
  * comma-delimited list of hosts (beginning with http:// or https://) or    *
  * "*" to allow all domains CORS access.                                    *
  *                                                                          *
  ***************************************************************************/

   origin: '*',

  /***************************************************************************
  *                                                                          *
  * Allow cookies to be shared for CORS requests?                            *
  *                                                                          *
  ***************************************************************************/

   credentials: true

  /***************************************************************************
  *                                                                          *
  * Which methods should be allowed for CORS requests? This is only used in  *
  * response to preflight requests (see article linked above for more info)  *
  *                                                                          *
  ***************************************************************************/

  // methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD',

  /***************************************************************************
  *                                                                          *
  * Which headers should be allowed for CORS requests? This is only used in  *
  * response to preflight requests.                                          *
  *                                                                          *
  ***************************************************************************/

  // headers: 'origin, content-type, accept'

};

【讨论】:

    【解决方案2】:

    您是否尝试过在 /config/cors.js 中设置 originmethods

    您也可以在此页面上找到更多信息Sails.Config.CORS

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-24
    • 2015-06-17
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 2011-10-27
    • 2013-06-27
    相关资源
    最近更新 更多