javascript - 访问控制来源不允许

标签 javascript node.js angularjs

<分区>

我在 AngularJS 中面临“不允许访问控制来源”的问题,基于 AngularJS CORS Issues我在我的代码中添加了以下几行:

angular.module('app', [
    'ngRoute',
    'appModules.filters',
    'appModules.ajaxCallService'
])
.config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) {

    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];

    $routeProvider.when('/login', {templateUrl: 'views/login.html', controller: 'LoginCtrl'});
}]);

但我仍然面临问题:

XMLHttpRequest cannot load http://127.0.0.1:8000/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

谁能帮我解决这个问题?

即使在 URL 中使用 localhost 我也会遇到同样的问题

XMLHttpRequest cannot load http://localhost:8000/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

我正在使用 AngularJS 版本 1.2.16 和带有 0.10.26 nodejs 的 expressJS。

最佳答案

我的问题解决了我已经添加了

  res.setHeader('Access-Control-Allow-Origin', 'http://localhost');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
  res.setHeader('Access-Control-Allow-Credentials', true);

在我的 expressJS 代码中,同时向客户端应用程序发送数据。

关于javascript - 访问控制来源不允许,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23469747/

相关文章:

javascript - 根据父 div 的 Canvas 数量调整 Canvas 大小

javascript - 构造函数如何调用自己的原型(prototype)

javascript - 在带有 View (路由)的 AngularJS 中,为什么 Controller 只向前执行?

javascript - 如何在 Gulp 中将文件和管道包装到同一目录中的新文件

node.js - 在 Node 中进行集成测试后清理 mongodb

css - 如何在 layout = row/column in angular material 中 float 元素?

angularjs - Angular JS动态地将列添加到ui-grid

javascript - jQuery 找不到附加元素

javascript - Three.js:我的飞机有黑暗的一面

javascript - 如何在 ElectronJS 中打印 DIV