I am trying to get data from url using http.post function. But I get errors. Download source package 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]);
JSON in url looks like this. When I click the button, I get errors that look like this:
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:
at AbstractChangeDetector.ChangeDetector_AppComponent_0.handleEventInternal (eval at <anonymous> (http:
at AbstractChangeDetector.handleEvent (http:
at AppView.dispatchEvent (http:
at AppView.dispatchRenderEvent (http:
at DefaultRenderView.dispatchRenderEvent (http:
at eventDispatcher (http:
at http:
at http:
at http:
ERROR CONTEXT:
[object Object]run @ angular2-polyfills.js:143zoneBoundFn @ angular2-polyfills.js:111
source
share