简体   繁体   English

Chrome中的AngularJS CORS问题-所请求的资源上没有“ Access-Control-Allow-Origin”标头

[英]AngularJS CORS issue in Chrome - No 'Access-Control-Allow-Origin' header is present on the requested resource

I'm doing a little exercise to familiarise myself with AngularJS and I have my app running on http://127.0.0.1:9000/ after I execute the necessary grunt task. 我正在做一些练习以熟悉AngularJS,并在执行必要的grunt任务后在http://127.0.0.1:9000/运行我的应用程序。 Now I wish to write/teach myself how to allow a Authorization/Authentication request (basically a login form). 现在,我想写/教自己如何允许一个授权/身份验证请求(基本上是一个登录表单)。 I have a seperate project that has a REST API, this is running on http://127.0.0.1:3000/ - notice the difference in port numbers. 我有一个具有REST API的单独项目,该项目在http://127.0.0.1:3000/上运行-请注意端口号的不同。 The exercise is hosted on my local machine so I wish to use CORS for my requests as browser restrictions aren't an issue and the app is for my own amusement. 该练习托管在我的本地计算机上,因此我希望使用CORS进行请求,因为浏览器限制不是问题,并且该应用是出于我的娱乐目的。 In my angularJS app I have included the following code in my config to allow CORS requests: 在我的angularJS应用中,我在config包含以下代码以允许CORS请求:

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

In my Rest API / Node Project I include the CORS middleware available from https://www.npmjs.org/package/cors and I have enabled CORS for all requests, I have included the library like so: 在我的Rest API / Node项目中,我包括可从https://www.npmjs.org/package/cors获得的CORS中间件,并且已为所有请求启用了CORS,我包括了这样的库:

cors = require('cors')

and then... 接着...

app.use(cors()); // this is in my app.configure

When I test the API using the Websore REST tool the data I desire is retured however when trying to access this is my AngularJS app the Chrome JavaScript console gives me the following error: 当我使用Websore REST工具测试API时,我想要的数据将被重新记录,但是当尝试访问它是我的AngularJS应用时,Chrome JavaScript控制台给我以下错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource

Is there anything I have missed with setting up CORS for AngularJS? 我为AngularJS设置CORS有什么想念的吗? Thanks in advance. 提前致谢。

Phew, I seem to have fixed in... In my app I needed to put the requirements in the following order: ew,我似乎已经修复了...在我的应用中,我需要按以下顺序排列需求:

app.use(cors());
app.use(app.router);

The app.use(cors()); app.use(cors()); must come before the app.use(app.router); 必须在app.use(app.router);之前出现app.use(app.router);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CORS所请求的资源上没有“ Access-Control-Allow-Origin”标头 - CORS No 'Access-Control-Allow-Origin' header is present on the requested resource AngularJS:所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - AngularJS: No 'Access-Control-Allow-Origin' header is present on the requested resource React - CORS 问题请求的资源上不存在“Access-Control-Allow-Origin”标头 - React - CORS issue No 'Access-Control-Allow-Origin' header is present on the requested resource CORS问题-“所请求的资源上没有'Access-Control-Allow-Origin'标头。” - CORS-issue - “No 'Access-Control-Allow-Origin' header is present on the requested resource.” 带有Access-Control-Allow-Origin标头的Jquery + CORS +基本身份验证:所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - Jquery + CORS+ Basic Auth with Access-Control-Allow-Origin header: No 'Access-Control-Allow-Origin' header is present on the requested resource AngularJS:所请求的资源上不存在“ Access-Control-Allow-Origin”标头。 因此,不允许访问原始“空” - AngularJS : No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access CORS 策略阻止了对获取的访问:请求的资源上不存在“Access-Control-Allow-Origin”header - Access to fetch has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource localhost:4200 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头 - localhost:4200 has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource 获取已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头 - fetch has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource 如何在 couchDB 中添加 cors — 请求的资源上不存在“Access-Control-Allow-Origin”标头 - how to add cors in couchDB — No 'Access-Control-Allow-Origin' header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM