저는 2 번을 처음 사용하고 있으며, 다음 코드를 사용하여 데이터를 얻기 위해 Api에 http 요청을하려고합니다.
서비스 :
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을 (를) 찾을 수 없음 :콘솔에서 6,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
에서이 기본 URL을 제거하면 어떻게 spotifyAPI에 요청할 수 있습니까?
당신이 나에게 프록시를 제공 할 수 있습니다. config.json file @ kiiiinnnnnnnnnyyyy – Vignesh