如何为 angular 5/nodejs 启用 Access-Control-Allow-Origin? [英] How to enable Access-Control-Allow-Origin for angular 5/nodejs?
问题描述
阅读了许多包含访问控制允许来源"的方法,但没有一种对我有用.
Read many ways for including of 'Access-Control-Allow-Origin' and none worked for me.
我使用@angular/common/http 模块和外部 url 作为数据源.通过尝试获取数据,得到错误://///.................
I use @angular/common/http module and external url as data source. by the attempt to get data instead, get error: /////.................
Failed to load http://accounts.......com/accounts: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 503.
account.service.ts:
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { HttpClient, HttpParams } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { Account } from '../models/account';
const baseUrl : string = 'http://accounts..................com/';
const httpOptions : any = {
headers: new HttpHeaders({
//'Content-Type': 'application/json',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Origin': '*'
})
};
@Injectable()
export class AccountService {
private isUserLoggedIn;
private usreName;
private account : Account;
constructor(
private http: HttpClient,
private router: Router
) {}
logIn (credentials: any): Observable<Account> {
return this.http.get<Account>(baseUrl + 'accounts');
}
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { routing } from './routing';
import { AppComponent } from './app.component';
import { AppGlobal } from './app.global';
import { AccountComponent } from './components/account/account.component';
@NgModule({
declarations : [
AppComponent,
AccountComponent,
....
],
imports : [
routing,
BrowserModule,
HttpClientModule,
CookieModule.forRoot()
],
providers : [AccountService, AppGlobal],
bootstrap : [AppComponent]
})
export class AppModule { }
请帮忙
////////////////尝试修复1
//......
import { HttpHeaders} from '@angular/common/http';
//.......
logIn (credentials: any): Observable<Account> {
const headers = new HttpHeaders()
.append('Content-Type', 'application/json')
.append('Access-Control-Allow-Headers', 'Content-Type')
.append('Access-Control-Allow-Methods', 'GET')
.append('Access-Control-Allow-Origin', '*');
return this.http.get<Account>(baseUrl + 'accounts', {headers});
}
我仍然收到那个错误:
对预检请求的响应未通过访问控制检查:请求的资源上不存在Access-Control-Allow-Origin"标头.Origin 'http://localhost:4200' 因此不允许访问.响应的 HTTP 状态代码为 503.
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 503.
////////////////尝试修复2
proxy.conf.json:
proxy.conf.json:
{
"/api": {
"target": "http://localhost:4200",
"secure": false,
"pathRewrite": {
"^/api": ""
},
"changeOrigin": true,
"logLevel": "debug"
}
}
ng serve --proxy-config proxy.conf.json
ng serve --proxy-config proxy.conf.json
也是错误
推荐答案
**Set headers to allow CORS origin in Express **
**Set headers to allow CORS origin in Express **
=> 在 server.js 文件或邮件文件中添加代码.
=> Add code in the server.js file or mail file.
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
CORS(跨源资源共享)是一项 HTML5 功能,允许一个站点访问另一个站点的资源,尽管它们位于不同的域名下.
CORS (Cross-Origin Resource Sharing) is an HTML5 feature that allows one site to access another site’s resources despite being under different domain names.
CORS 的 W3C 规范实际上在提供响应标头的一些简单示例方面做得非常好,例如密钥标头、Access-Control-Allow-Origin 和其他必须用于在您的设备上启用 CORS 的标头.网络服务器.
The W3C specification for CORS actually does a pretty good job of providing some simple examples of the response headers, such as the key header, Access-Control-Allow-Origin, and other headers that you must use to enable CORS on your web server.
这篇关于如何为 angular 5/nodejs 启用 Access-Control-Allow-Origin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!