Tôi không thể thay đổi tiêu đề khi thực hiện yêu cầu POST. Tôi đã thử một vài điều:Đầu đề Angular2/Http (POST)
Simple lớp:
export class HttpService {
constructor(http: Http) {
this._http = http;
}
}
tôi đã cố gắng:
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'); }
);
}
không ai trong số hai đang làm việc. Tôi không quên nhập bất kỳ lớp nào.
Tôi đang sử dụng Google Chrome. Vì vậy, tôi kiểm tra tab 'Mạng', yêu cầu của tôi ở đó và nội dung của tôi là Loại văn bản/đồng bằng.
Đây có phải là lỗi hoặc tôi đang làm điều gì sai?
CẬP NHẬT Tôi quên để import lớp Headers từ Angular2/http:
import {Headers} from 'angular2/http';
bạn có muốn tạo plunkr không? Tùy chọn đầu tiên có vẻ tốt, vì vậy về cơ bản nó sẽ hoạt động ... – eesdil