问题描述
在NodeJS后端,我已将此代码添加到Server.js
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(); });
但在AngularJS 2中,Google Chrome中的客户端抛出此错误
XMLHttpRequest cannot load http://localhost:8080/team. 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:3000' is therefore not allowed access. The response had HTTP status code 405.
这是Angular2的服务代码我正在使用
export class DataService { // URL to web api constructor(private http: Http, private _configuration: Configuration,private team:Team) { //this.actionUrl = _configuration.ServerWithApiUrl; this.actionUrl = "http://localhost:8080/team"; } private actionUrl: string; addData(postData: Team): Observable<Team[]> { //let body = JSON.stringify({ Team }); this.team = postData; console.log(this.team); let body = JSON.stringify({ postData }); let headers = new Headers({ 'Content-Type': 'application/json' }); let options = new RequestOptions({ headers: headers }); console.log(body); return this.http.post(this.actionUrl, body, options) .map(this.extractData) .catch(this.handleError); }
推荐答案
更新:
为您的新错误消息:Angular2是较低套管的标题. 请更新您的后端以接受content-type.
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, content-type, Accept");
您无法测试帖子到此URL: http://posttestserver.com/post.php? dir = anynameyouwillfind
并可以看到您的帖子: http://posttestserver.com/data/
浏览到年,月,日和任何nounamyouwillfind.
旧:
您必须为您的URL进行前缀!!
this.http.get(' http ://localhost:8080/v1_user/45646/team');
其他推荐答案
通过将此添加到后端节点.js
来固定它// access control --> allowed all origin 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"); // Request headers you wish to allow res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); next(); }) .options('*', function(req, res, next){ res.end(); }) ;
问题描述
in the nodejs backend i have added this code to the server.js
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(); });
but in angularjs 2 client side in google chrome is throwing this error
XMLHttpRequest cannot load http://localhost:8080/team. 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:3000' is therefore not allowed access. The response had HTTP status code 405.
this is the service code for angular2 i'm using
export class DataService { // URL to web api constructor(private http: Http, private _configuration: Configuration,private team:Team) { //this.actionUrl = _configuration.ServerWithApiUrl; this.actionUrl = "http://localhost:8080/team"; } private actionUrl: string; addData(postData: Team): Observable<Team[]> { //let body = JSON.stringify({ Team }); this.team = postData; console.log(this.team); let body = JSON.stringify({ postData }); let headers = new Headers({ 'Content-Type': 'application/json' }); let options = new RequestOptions({ headers: headers }); console.log(body); return this.http.post(this.actionUrl, body, options) .map(this.extractData) .catch(this.handleError); }
推荐答案
UPDATED:
For your new error message: Angular2 is lower-casing the headers. Please update your backend to accept content-type too.
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, content-type, Accept");
You cant test post's to this URL: http://posttestserver.com/post.php?dir=anyNameYouWillFind
And can see your posts there: http://posttestserver.com/data/
Browse to year, month, day and anyNameYouWillFind ..
OLD:
you have to prefix your url!!
this.http.get('http://localhost:8080/v1_user/45646/team');
其他推荐答案
Fixed it by adding this to the backend node.js
// access control --> allowed all origin 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"); // Request headers you wish to allow res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); next(); }) .options('*', function(req, res, next){ res.end(); }) ;