正在使用REST Api

时间:2016-03-11 17:00:34

标签: rest http angular

我正在努力在Angular 2中制作REST Api请求。

我的API本身在消费时起作用:

http://localhost:8080/tshirts

它会让我回头:

[{
  "Id": 1,
  "Title": "honey blanket",
  "PrintTechnique": "sublimation"
}, {
  "Id": 2,
  "Title": "tell your story",
  "PrintTechnique": "screen print"
}]

现在,我尝试在Angular中实现请求,受到documentation

的启发
import {Injectable, Injector} from 'angular2/core';
import {HTTP_PROVIDERS, Http, Request, RequestMethod} from 'angular2/http';

@Injectable()
export class TshirtService {
    constructor(public http:Http) {}

    getTshirts() {
        var tshirts = this.http.request(new Request({
            method: RequestMethod.Get,
            url: 'http://localhost:8080/tshirts'            
        }));
        console.log(tshirts);
        return tshirts;
    }        
}

根据我的界面,结果应该是Tshirts[]类型:

export interface Tshirt {
    Id: number;
    Title: string;
    PrintTechnique: string;
}

但是,控制台不会打印Tshirt对象,而是打印Observable

Observable {_isScalar: false}
  _isScalar:false
  _subscribe:(responseObserver)
  __proto__:Object

我做错了什么?

1 个答案:

答案 0 :(得分:-1)

我认为响应可观察性需要通过类似......

之类的方式进行映射
this.http.request(...)
.map(response => response.json());