Ich kann die Header nicht ändern, wenn Sie eine POST-Anfrage machen. Ich habe versucht, ein paar Dinge:Angular2/Http (POST) Header
Einfach Klasse:
export class HttpService {
constructor(http: Http) {
this._http = http;
}
}
Ich habe 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, irgendwelche der Klassen zu importieren.
Ich benutze Google Chrome. Also überprüfe ich die Registerkarte "Netzwerk", meine Anfrage ist dort und es heißt mein Inhaltstyp ist text/plain.
Ist das ein Fehler oder mache ich etwas falsch?
UPDATE Ich habe vergessen, die Header-Klasse von Angular2/http zu importieren:
import {Headers} from 'angular2/http';
würde es Ihnen etwas ausmachen plunkr zu erstellen? Die erste Option sieht gut aus, also im Grunde sollte es funktionieren ... – eesdil