2016-01-25 29 views
9

Ho guardato tutte le questioni GitHub, ei posti StackOverflow, ma sono in grado di farlo funzionareAngular2 - http.post (...) mappa non è una funzione

(. https://github.com/angular/angular/issues/5632)

(Angular 2 HTTP GET with TypeScript error http.get(...).map is not a function in [null])

ho provato diverse importazioni:

import 'rxjs/add/operator/map';

import 'rxjs/rx';

Ma io continuo a ricevere l'errore http.post(...).map is not a function

aggiornamento - contesto codice

let body = "email=" + email + "&password=" + password; 
let headers = new Headers(); 
headers.append('Content-Type', 'application/x-www-from-urlencoded'); 
this.http.post('http://angular.app/api/v1/auth') // angular.app is laravel backend 
     .map((responseData) => { 
      return responseData.json(); 
     }) 
+0

Potrebbe darci Moure dettagli sul codice intorno al vostro Chiamata HTTP? Grazie! –

+0

@ThierryTemplier vedi il mio aggiornamento –

+0

Grazie! Aggiungi 'HTTP_PROVIDERS' quando chiamate la funzione' bootstrap'? –

risposta

2

Sembra che Angular2 beta.1 richieda RxJS 5.0.0-beta.0. Forse è la causa del tuo problema.

Se provo questo nel mio file package .json:

"dependencies": { 
    "angular2": "2.0.0-beta.1", 
    "systemjs": "0.19.6", 
    "es6-promise": "^3.0.2", 
    "es6-shim": "^0.33.3", 
    "reflect-metadata": "0.1.2", 
    "rxjs": "5.0.0-beta.1", 
    "zone.js": "0.5.10" 
}, 

E ho l'errore che richiede Angular2 RxJS 5.0.0-beta.0.

Modifica

è necessario aggiungere il HTTP_PROVIDERS entro il secondo parametro della funzione bootstrap.

Spero che ti aiuta, Thierry

+0

Ho provato la versione beta.0, ancora senza fortuna, ottengo lo stesso errore ('ReferenceError: map is not defined') –

+0

@AngeloA hai provato con: 'import' rxjs/Rx ';' ('Rx' not' rx ')? –

+0

Sì, sì, (maiuscola R) –

6

Per me le http.post(...).map() funziona come previsto. ho bisogno di importazione 'rxjs/Rx'

import {Component} from 'angular2/core'; 
import {Http} from 'angular2/http'; 
import 'rxjs/Rx'; 
@Component({ 
    selector: 'my-app', 
    template: ` 
    <h1>{{title}}</h1> 
    <p>Test result {{result | json}}</p> 
    ` 
}) 
export class App implements OnInit{ 
public title = 'my title'; 
public result : String; 

constructor(private _http : Http) { 
    _http.post('http://jsonplaceholder.typicode.com/posts') 
    .map(res => res.json()) 
    .subscribe(
     data => this.result = data, 
     err => console.log('ERROR!!!'), 
    () => console.log('Got response from API', this.result) 
    ); 
    } 
} 

See plunker esempio: http://plnkr.co/edit/vise2zYxZUmr1kW65mNY?p=preview

spera che questo vi aiuterà a trovare il vostro porblem

+0

Questo ha funzionato per me. Stavo avendo lo stesso problema con '" @ angular/core ":"^4.0.0 "," rxjs ":"^5.1.0 "...' – Dev

Problemi correlati