如何在angular2中将头文件设置为application / json [英] how to set headers to application/json in angular2

查看:349
本文介绍了如何在angular2中将头文件设置为application / json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Angular2中发送HTTP发布请求,但无法将标头设置为内容类型应用程序JSON。

I am trying to send HTTP post request in Angular2 but not able to set headers to content type application 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,因此服务器没有收到任何数据。
任何建议将不胜感激。

When i checked in network i found that Content-Type is set as text/plain and thus server is not receiving any data. Any suggestions will be appreciated.

推荐答案

试试这个代码:

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());
}

这篇关于如何在angular2中将头文件设置为application / json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆