Angular2 / http(POST)

时间:2016-09-13 09:01:55

标签: angular2-http

我是Angular 2的新人,我被困在Angular Http帖子上。我把所有代码HERE`

addHero(name: string): Observable<Hero>{
        let body= JSON.stringify({name});
        let headers = new Headers();
        headers.append('Accept','application/json');
        headers.append('Content-Type','application/json');

        let options = new RequestOptions({ headers: headers });

        console.log(body);
        return this.http.post(this.heroesUrl, body, options).map(this.extractData).catch(this.handleError)
        //return new Observable(name=>{id:3; name:name})
    }

`

当我点击添加英雄按钮时,它会抛出一个错误,如下图所示 enter image description here 任何人都知道这有什么问题。提前谢谢

1 个答案:

答案 0 :(得分:0)

要发布数据,您需要创建API或模拟API。根据文档,您只能使用.json文件中的get。

对于学习angular 2 http我会建议在教程中设置内存api。请在此处查看InMemoryDataService:https://angular.io/docs/ts/latest/tutorial/toh-pt6.html

更多关于它如何运作:https://angular.io/docs/ts/latest/guide/server-communication.html#!#in-mem-web-api

教程中的工作人员:https://angular.io/resources/live-examples/toh-6/ts/plnkr.html

相关问题