javascript - 从节点到角度的数据检索时出现 Cors 错误
问题描述
我收到错误
从“ http://localhost:4040/admin ”重定向到“ http://localhost:4040/admin/ ”已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头. 因此,不允许访问源“ http://localhost:4200 ”。
我的 Angular 6 服务器在端口 4200 上,节点在端口 4040 上。
我已经尝试了一切,这是迄今为止让我受益的代码。
节点JS
app.use(function(req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', "access-control-allow-origin, origin,accept, X-Requested-With, content-type, Access-Control-Request-Method, Access-Control-Request-Headers");
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
app.get('/admin', async function(req, res) {
res.send({
data: "HI"
});
}
角度数据服务
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class DataService {
origin = "http://localhost:4040";
constructor(public http:HttpClient) {
}
getAdminData(){
const headers = new HttpHeaders()
.set('Access-Control-Allow-Origin', '*')
.set('Content-Type', 'application/json');
return this.http.get(this.origin+'/admin',
{
headers: headers
})}
角组件
import { Component, OnInit } from '@angular/core';
import {Data} from "../../interfaces/dashboard.interface";
import { DataService } from "../../services/data.service";
@Component({
selector: 'app-admindashboard',
templateUrl: './admindashboard.component.html',
styleUrls: ['./admindashboard.component.css']
})
export class AdmindashboardComponent implements OnInit {
Data: Data[];
constructor(private dataService:DataService) { }
ngOnInit() {
console.log('HERE');
this.dataService.getAdminData().subscribe((res) => {
console.log(res);
// this.posts = posts;
// for(let i = this.currentLimit; i < this.pageLimit; i++){
// console.log(this.somePosts, posts[i]);
// this.somePosts.push(this.posts[i]);
// this.currentLimit = 10;
// }
});
}
}
解决方案
更新。尝试这个:
// Website you wish to allow to connect
res.header('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.header('Access-Control-Allow-Headers', 'Accept, Content-Type, X-Requested-With', 'X-HTTP-Method-Override');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.header('Access-Control-Allow-Credentials', true);
if (req.method === 'OPTIONS') {
res.sendStatus(200);
} else {
next();
}
在您的客户端应用程序中,不要设置任何标题
推荐阅读
- java - 我如何有效地禁止用户使用 Spring Web 应用程序
- python - 使用数据框将列数据显示为单个列
- quarkus - 如何使用 web.xml 作为部署描述符本地构建基于 servlet 的 Quarkus 应用程序?
- c# - 子节点未正确序列化
- geofirestore - 如何处理 GeoFirestore 查询的读取操作?
- nand2tetris - 在第 0 行,表达式预期错误(Nand2tetris CPU-emulator)
- bash - 在 Mac OSX 上从 bash 脚本运行时出现 imagemagick 错误
- javascript - 如何记忆函数,以便从缓存中返回相同的参数
- ios - 如何将您的应用程序注册为“复制到”而不是“打开方式”到某个文档类型
- google-maps - Android - 如何通过意图触发具有多个航点的独立导航应用程序?