我无法在进行POST请求时更改头文件。我尝试了几种方法:
简单的类:
export class HttpService {
constructor(http: Http) {
this._http = http;
}
}
我尝试过:
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'); }
);
}
两个都不起作用。我没有忘记导入任何一个类。
我在使用 Google Chrome。所以我检查了“网络”选项卡,我的请求在那里,并且它说我的Content-Type是text/plain。
这是一个错误还是我做错了什么?
更新:我忘记从Angular2 / http中导入头文件类:
import {Headers} from 'angular2/http';