Non riesco a modificare le intestazioni quando faccio una richiesta POST. Ho provato un paio di cose:Intestazioni Angular2/Http (POST)
classe Semplice:
export class HttpService {
constructor(http: Http) {
this._http = http;
}
}
ho provato:
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'); }
);
}
nessuno dei due stanno lavorando. Non ho dimenticato di importare nessuna delle classi.
Sto utilizzando Google Chrome. Quindi controllo la scheda "Rete", la mia richiesta è lì e dice che il mio Content-Type è text/plain.
È un errore o sto facendo qualcosa di sbagliato?
UPDATE Ho dimenticato di importare la classe intestazioni da Angular2/http:
import {Headers} from 'angular2/http';
ti dispiacerebbe creare plunkr? La prima opzione sembra buona, quindi in pratica dovrebbe funzionare ... – eesdil