我是新来的角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