我是新來的角2,我試圖讓一個HTTP請求到Spotify的API使用以下代碼來獲取數據。
服務:
import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class SpotifyService{
private searchUrl: string;
constructor(private _http:Http){
// this.searchUrl = '';
}
searchMusic(str:string, type="artist"){
this.searchUrl = `https://api.spotify.com/v1/search?query=${str}&offset=0&limit=20&type=${type}&market=US`;
console.log(this.searchUrl);
return this._http.get(this.searchUrl).map(res => res.json());
}
}
組件:
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
import {SpotifyService} from '../../services/spotify.service';
@Component({
moduleId: module.id,
selector: 'search',
templateUrl: 'search.component.html',
providers: [SpotifyService]
})
export class SearchComponent {
searchStr:string;
constructor(private _spotifyService:SpotifyService){
}
searchMusic(){
//console.log(this.searchStr);
this._spotifyService.searchMusic(this.searchStr).subscribe(res => { console.log(res.artist.items)});
}
}
雖然發出請求的API,我收到此錯誤
例外:響應,狀態:404找不到網址: http://localhost:3000/https/api.spotify.com/v1/search?query=a&offset=0&limit=20&type=artist&market=US
在控制檯中,的this.searchUrl
值是
https://api.spotify.com/v1/search?query=a&offset=0&limit=20&type=artist&market=US
但同時使請求其在它的前面添加鹼URL即http://localhost:3000
。如何從searchUrl
中刪除此基礎網址,以便我可以向spotifyAPI發出請求。
你能提供給我的代理。 config.json file @ kiiiinnnnnnnnnyyyy – Vignesh