【发布时间】:2020-09-05 20:01:24
【问题描述】:
我正在开发一个项目,我需要访问我开发的 Api
const endpoint = 'localhost:3000/api/v1/';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
}),
};
@Injectable({
providedIn: 'root',
})
export class RestService {
constructor(private http: HttpClient) {}
private extractData(res: Response) {
let body = res;
return body || {};
}
getRequest(id: String): Observable<Request> {
return this.http.get<Request>(endpoint + 'request/' + id, httpOptions);
}
我已经授予权限以便我可以访问它,但我收到此错误
ccess to XMLHttpRequest at 'localhost:3000/api/v1/request/5ec3fbd84fe7bf4aa8d48b39' from origin 'http://localhost:4200' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
这里是权限:
app.use(
cors({
credentials: true,
origin: "http://localhost:4200",
})
);
找到了解决办法。端点应该是'http://localhost:3000/api/v1/'
【问题讨论】:
标签: node.js angular express cors