Node.js Spotify API的CORS重定向问题?
我的web应用程序前端使用Angular 12,后端使用Node.js 12.16.0 w/Express.js。我正在尝试开发一个web应用程序,允许用户使用他们的Spotify API登录,但即使在查找了大量帖子之后,我在CORS方面也遇到了很多麻烦。在前端,我的代码只是使用HttpClient调用我的Node.js Spotify API的CORS重定向问题?,node.js,angular,express,cors,spotify,Node.js,Angular,Express,Cors,Spotify,我的web应用程序前端使用Angular 12,后端使用Node.js 12.16.0 w/Express.js。我正在尝试开发一个web应用程序,允许用户使用他们的Spotify API登录,但即使在查找了大量帖子之后,我在CORS方面也遇到了很多麻烦。在前端,我的代码只是使用HttpClient调用我的/login端点 public login(): Observable<any> { const url = backendURL + BackendEndpoints.L
/login
端点
public login(): Observable<any> {
const url = backendURL + BackendEndpoints.Login;
return this.http.get<any>(url, {withCredentials: true});
}
我还在中间件中使用cors包
this.expressApp.use(cors({origin: 'http://localhost:4200', credentials: true}));
我的前端没有重定向,而是得到这个CORS错误
跨源资源共享策略拒绝跨源重定向到:访问控制允许源不允许源null
为什么我仍然会犯CORS错误?如果可能的话,我只想配置我的后端,而不是创建代理服务器。编辑:不工作
您是否尝试在角度调用的标题中添加原点?
诸如此类:
public login(): Observable<any> {
const url = backendURL + BackendEndpoints.Login;
const header = new HttpHeaders();
header.append('Origin', 'http://localhost:4200');
return this.http.get<any>(url, {withCredentials: true, headers: header});
}
希望这项工作已经试过了。现在我得到一个不同的错误:“[Spotify API Link]被跨源资源共享策略拒绝:访问控制不允许源允许源”我发现了一些可能会对你有帮助的东西:他们说使用作为你可以尝试的例子:这是可行的,但我更愿意在前端调用函数,因为我想在我的应用程序和缓存中生成一些东西。我用一个答案编辑,希望这能奏效:)
public login(): Observable<any> {
const url = backendURL + BackendEndpoints.Login;
const header = new HttpHeaders();
header.append('Origin', 'http://localhost:4200');
return this.http.get<any>(url, {withCredentials: true, headers: header});
}
<a href="/login" #spotifyConnectLink></a>
<button (click)='handleCacheAndLaunchConnection()'>Connect To Spotify</button>
@ViewChild('spotifyConnectLink') spotifyConnectLink: ElementRef;
handleCacheAndLaunchConnection() {
//Do the stuff you need for app and cache
let el: HTMLElement = this.spotifyConnectLink.nativeElement;
el.click();
}