Angular 2中的Http.post错误:map不是一个函数

4

我正在尝试通过http.post函数从url获取数据。但是我遇到了错误。

以下是app.component.ts文件的源代码:

import {Component} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {Http, HTTP_PROVIDERS, Headers} from 'angular2/http';
@Component({
selector: 'my-app',
template: `
 <div>
  <button (click)="postData()">Post Data</button>
  <p>Posted the following values:</p>
  <div>{{postResponse.id}}</div>
  <div>{{postResponse.name}}</div>
 </div>`
})
export class AppComponent {
 result: Object;
 http: Http;
 postResponse = new Person();

 constructor(http: Http) {
    this.http = http;
 }

 postData(){
    var headers = new Headers();
    headers.append('Content-Type', 'application/json');

    this.http.post('http://141.79.39.454:8008/the-link-something-like-this...', JSON.stringify({"id": 1, "name": "Новые"}),{headers:headers})
    .map(res => res.json())
    .subscribe((res:Person) => this.postResponse = res);
 }
}

class Person{
 id:number;
 name:string;
}

bootstrap(AppComponent, [HTTP_PROVIDERS]);

在url中,JSON的格式看起来像这样。 当我点击按钮时,出现的错误信息看起来是这样的:

EXCEPTION: Error during evaluation of "click"
Uncaught EXCEPTION: Error during evaluation of "click"
ORIGINAL EXCEPTION: TypeError: this.http.post(...).map is not a function
ORIGINAL STACKTRACE:
TypeError: this.http.post(...).map is not a function
at AppComponent.postData (http://localhost:3000/app/app.component.js:35:26)
at AbstractChangeDetector.ChangeDetector_AppComponent_0.handleEventInternal (eval at <anonymous> (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:10897:14), <anonymous>:124:33)
at AbstractChangeDetector.handleEvent (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:8788:22)
at AppView.dispatchEvent (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:9396:39)
at AppView.dispatchRenderEvent (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:9391:19)
at DefaultRenderView.dispatchRenderEvent (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:7819:53)
at eventDispatcher (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:9781:19)
at http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:9852:14
at http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:15370:34
at http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:7874:18
ERROR CONTEXT:
[object Object]run @ angular2-polyfills.js:143zoneBoundFn @ angular2-polyfills.js:111

请将堆栈跟踪作为文本发布在问题中,而不是链接到图像。 - JB Nizet
你能看一下吗? - Nursultan Bagidolla
我认为你只是在使用旧版本的Angular2。请更新到最新的beta版。 - JB Nizet
我已经安装了最新的测试版。 - Nursultan Bagidolla
那我就不知道了。 - JB Nizet
1个回答

12

哦,错误消失了!)但现在它不显示请求的结果。这可能与标题或JSON.stringify有关吗? - Nursultan Bagidolla
一个提示是查看 Chrome 开发工具中的“网络”选项卡,例如查看请求是否被执行并且响应是否存在。 - Thierry Templier

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接