Angular2/Http (POST) - Header

Ich bin nicht in der Lage zu ändern, den Header, wenn eine POST-Anforderung. Ich habe versucht, ein paar Dinge:

Einfach Klasse:

export class HttpService {
    constructor(http: Http) {
        this._http = http;
    }
}

Habe ich versucht:

testCall() {
    let body = JSON.stringify(
        { "username": "test", "password": "abc123" }
    )

    let headers = new Headers();
    headers.append('Content-Type', 'application/json'); //also tried other types to test if its working with other types, but no luck

    this._http.post('http://mybackend.local/api/auth', body, { 
        headers: headers 
    })
    .subscribe(
        data => { console.log(data); },
        err => { console.log(err); },
        {} => { console.log('complete'); }
    );
}

2:

testCall() {
    let body = JSON.stringify(
        { "username": "test", "password": "abc123" }
    )

    let headers = new Headers();
    headers.append('Content-Type', 'application/json'); //also tried other types to test if its working with other types, but no luck

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

    this._http.post('http://mybackend.local/api/auth', body, options)
    .subscribe(
        data => { console.log(data); },
        err => { console.log(err); },
        {} => { console.log('complete'); }
    );
}

keiner der beiden arbeiten. Ich habe nicht vergessen zu importieren einer der Klassen.

Ich bin mit Google Chrome. Also ich check das 'Network' - tab, meine bitte ist es, und es sagt, dass mein Content-Type ist text/plain.

Ist das ein bug oder mache ich etwas falsch?

UPDATE
Ich vergaß zu importieren die Header-Klasse aus Angular2/http:

import {Headers} from 'angular2/http';

InformationsquelleAutor der Frage Angelo A | 2016-01-27

Schreibe einen Kommentar