我正在尝试在 Angular 2 中发送 HTTP post 请求,但无法将标头设置为内容类型应用程序 JSON。
我的代码是:
login(url,postdata)
{
var headers = new Headers({'Content-Type': 'application/json'});
return this._http.post(url,JSON.stringify(postdata),this.headers)
.map(res => res.json())
}
当我检查网络时我发现Content-Type
被设置为text/plain
因此服务器没有接收任何数据。
任何建议将不胜感激。
试试这个代码:
private _getHeaders():Headers {
let header = new Headers({
'Content-Type': 'application/json'
});
return header;
}
public login(url, postdata){
let options = new RequestOptions({
headers: this._getHeaders()
});
return this.http.post(url, JSON.stringify(postdata),options).map(res => res.json());
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)