我試圖在我的Angular 4應用程序中使用Spotify/Musixmatch API獲取一些數據,但它不工作。我不斷收到此錯誤:Angular 4 - 沒有'Access-Control-Allow-Origin'標題出現在請求的資源上
XMLHttpRequest cannot load http://api.musixmatch.com/ws/1.1/album.get?album_id=14250417&apikey=xyz010xyz . 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:9000 ' is therefore not allowed access.
JS
let headers = new Headers();
headers.append('Content-Type','application/json');
headers.append('Accept', 'application/json');
headers.append('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, DELETE, PUT');
headers.append('Access-Control-Allow-Origin', '*');
headers.append('Access-Control-Allow-Headers', "X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding");
let options = new RequestOptions({ headers: headers });
console.log(options)
return this.http.get(this.url + 'album.get?album_id=' + album_id + '&apikey=' + this.apikey, options)
.map(res => res.json())
}
的頭需要在後端設置,而不是前端... – Alex
@ AJT_82我使用Spotify的API。 –
從來沒有使用它,所以不能真的幫助那裏...你有沒有嘗試過這樣的事情? https://stackoverflow.com/a/41879977 – Alex