Nie mogę zmienić nagłówków podczas wykonywania żądania POST. Próbowałem kilka rzeczy:Nagłówki Angular2/Http (POST)
prostą klasę:
export class HttpService {
constructor(http: Http) {
this._http = http;
}
}
Próbowałem:
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'); }
);
}
żaden z nich pracują. Nie zapomniałem zaimportować żadnej z klas.
Używam Google Chrome. Sprawdzam więc kartę "Sieć", moje żądanie jest tam i mówi, że mój typ treści to tekst/zwykły.
Czy to błąd, czy też robię coś nie tak?
UPDATE zapomniałem zaimportować klasę Nagłówki z Angular2/http:
import {Headers} from 'angular2/http';
Czy mógłbyś stworzyć plunkr? Pierwsza opcja wygląda dobrze, więc w zasadzie powinna zadziałać ... – eesdil