我想通过API创建一些JSON数据,但它没有收到它。我用下面的代码角:从API获取JSON数据与Angular 2
getBook(id: string){
return this._http.get(this.url + 'books/' + id)
.map(res => {
console.log(res.json()); //It does not show anything
return res.json();
})
然而getBooks()方法没有任何问题获取数据。浏览器控制台中没有错误。 这是整个服务代码:
import { Injectable } from '@angular/core';
import { Http } from "@angular/http";
import 'rxjs/add/operator/map';
import { Observable } from "rxjs/Observable";
@Injectable()
export class LibrosService {
url: string = "http://localhost/API/public/index.php/api/";
constructor(private _http: Http) { }
getBooks(){
return this._http.get(this.url + 'books')
.map(res => res.json()); //it works
}
getBook(id: string){
return this._http.get(this.url + 'books/' + id)
.map(res => {
console.log(res.json()); //it does not work
return res.json();
})
}
对不起,我的英语,如果它不是很好,感谢你的帮助。
您是否订阅了可观察的?换句话说,如果你只是附加一个.subscribe()到getBook get,那它会执行吗? – JSess
是的,我在订阅它,但它没有收到数据。 – Sh13