AngularJS/SailsJS :Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers

标签 angularjs sails.js

我正在 Angular js和sails.js(node.js框架)之间的cors问题上挣扎

我尝试修复错误:XMLHttpRequest无法加载http://localhost:1337/en/auth/forgetpass/email。 Access-Control-Allow-Headers不允许请求 header 字段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网络标签页的结果是:
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

而且我仍然遇到相同的错误。一个主意?

非常感谢!

最佳答案

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

我将响应和请求 header 与拦截器以及它进行了比较。

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

在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设置配置中,我评论了方法和 header 。而且效果很好。
    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'

};

关于AngularJS/SailsJS :Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29596187/

相关文章:

angularjs - Ng-Repeat 内的 Angular JS 日期格式过滤器不格式化

node.js - Sails.js 和 Mocha、res.view 测试

javascript - 为什么我不需要在 Sails.js Controller 函数中调用 next,即使它的主体中有一个(异步?)数据库查询?

node.js - 风 sails .js : File upload not working

javascript - AngularJS +sails.js

AngularJS - 以 Angular 声明服务的不同方式的主要区别是什么?

angularjs - 运行 npm install -g yo 命令给出 ->"npm ERR! Windows_NT 6.1.7601"

javascript - 在对象内使用方括号 : Unexpected Token

sails.js - Waterline/Sails.js/Mongo 如何使用 AND 条件查询一个文件?

javascript - 基于下拉选择的angularJS中的条件模型绑定(bind)