Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Spotify API的CORS重定向问题?_Node.js_Angular_Express_Cors_Spotify - Fatal编程技术网

Node.js Spotify API的CORS重定向问题?

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

我的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.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();
 }