我试图用angular2发送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())
}

当我签入网络时,我发现内容类型设置为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());
}

关于angular - 如何在angular2中将 header 设置为application/json,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44006844/

10-12 00:27
查看更多