Javascript 从Api返回的角度为5/6的搜索数据
我有一个从外部api返回电影数据的api,我已经在我的api中实现了一个搜索功能,以下是我到目前为止得到的: 组件1.ts:Javascript 从Api返回的角度为5/6的搜索数据,javascript,node.js,angular,html,typescript,Javascript,Node.js,Angular,Html,Typescript,我有一个从外部api返回电影数据的api,我已经在我的api中实现了一个搜索功能,以下是我到目前为止得到的: 组件1.ts: export class MoviesComponent implements OnInit { movies: any; searchRes: any; searchStr: any; constructor(private router: ActivatedRoute, private http: Http, private location: L
export class MoviesComponent implements OnInit {
movies: any;
searchRes: any;
searchStr: any;
constructor(private router: ActivatedRoute, private http: Http, private location: Location, private moviesService: MoviesService) {
this.movies = [];
}
searchMovies() {
this.moviesService.searchMovies(this.searchStr).then(res => {
this.searchRes = res.results;
});
}
}
service.ts:
export class MoviesService {
searchStr: string;
private apiUrl = 'http://localhost:8000/movies';
constructor(private http: Http, private _jsonp: Jsonp) { }
searchMovies(searchStr: string): Promise<any> {
return this.http.get(this.apiUrl)
.toPromise()
.then(this.handleData)
.catch(this.handleError);
}
private handleData(res: any) {
const body = res.json();
console.log(body); // for development purposes only
return body.result || body || { };
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error); // for development purposes only
return Promise.reject(error.message || error);
}
}
}
导出类电影服务{
searchStr:string;
私人apiUrl服务器http://localhost:8000/movies';
构造函数(私有http:http,私有_jsonp:jsonp){}
searchMovies(searchStr:string):承诺
当我运行应用程序时,一切正常,当我尝试搜索例如复仇者时,它会显示所有电影,而不仅仅是goea命名为复仇者的电影,我在这里做错了什么?上述问题是,即使你将
searchStr
作为方法的参数传递给ng>api
searchMovies(searchStr: string): Promise<any> {
return this.http.get(this.apiUrl) //pass **`searchStr`** here
.toPromise()
.then(this.handleData)
.catch(this.handleError);
searchMovies(searchStr:string):承诺{
返回this.http.get(this.apirl)//pass**`searchStr`**此处
.toPromise()
.然后(这是handleData)
.接住(这个.把手错误);
`searchMovies(searchStr:string):Promise{返回this.http.get(this.apirl+searchStr).toPromise().then(this.handleData.catch(this.handleError);}`这在我的页面中没有显示任何内容,只是加载资源失败错误:服务器响应状态为404(未找到)都德!你需要检查你的api的url并相应地匹配请求,我已经提到了问题你的api是什么样子的?我添加了如下内容:queryUrl='?search=';
和searchMovies(searchStr:string):承诺{返回这个.http.get(this.apirl+this.queryUrl+searchStr).toPromise().then(this.handleData).catch(this.handleError);}
我仍然有所有的电影,我被卡住了:(抱歉耽误了你的时间兄弟
searchMovies(searchStr: string): Promise<any> {
return this.http.get(this.apiUrl) //pass **`searchStr`** here
.toPromise()
.then(this.handleData)
.catch(this.handleError);