我想通過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