typescript 如何在 angular2 中将标题设置为 application/json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44006844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:33:19  来源:igfitidea点击:

how to set headers to application/json in angular2

angulartypescriptpostheader

提问by palak

I am trying to send HTTP post request in Angular2 but not able to set headers to content type application JSON.

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

My code is:

我的代码是:

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

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.

当我检查网络时,我发现 Content-Type 设置为 text/plain,因此服务器没有接收任何数据。任何建议将不胜感激。

回答by monica

Try this code:

试试这个代码:

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

回答by dev07

change _http.postparameters :

更改_http.post参数:

login(url,postdata) {
    var headers = new Headers({'Content-Type': 'application/json'});
    return this._http.post(url,postdata,{headers:headers})
                .map(res => res.json())    
}

回答by Roman C

You should use code like this

你应该使用这样的代码

let headers = new Headers();
headers.append('Content-Type', 'application/json');

let options = new RequestOptions({ headers: headers });
return this.http.post(url, JSON.stringify(postdata), options).map(res => res.json());

回答by theTechRebel

Referencing the Angular 2 Angular Http Guide@angular/http has been deprecated, and @angular/common/http should be the one you are using in your Angular 2 App. Because if you do not specify http headers the default request will be sent as Content-Type text/plain, How you modify the http headers is to:

参考 Angular 2 Angular Http 指南@angular/http 已被弃用,@angular/common/http 应该是您在 Angular 2 应用程序中使用的那个。因为如果不指定 http 标头,默认请求将作为 Content-Type text/plain 发送,修改 http 标头的方法是:

import { HttpClient, HttpHeaders } from '@angular/common/http';
.....
const req = this.http.post('/api/PostRequest/',
                            JSON.stringify(object),
                            {
                             headers:new HttpHeaders()
                             .set('Content-Type','application/json')
                             }).subscribe();

回答by Ricardo Saracino

Angular 9 version for 2020

2020 年的 Angular 9 版本

export class ApiService {

  private headers = new HttpHeaders({
    'Content-Type': 'application/json',
  });

  constructor(
    private http: HttpClient) {
  }

  get(path: string, params: HttpParams = new HttpParams()): Observable<any> {
    return this.http.get(`${environment.apiUrl}${path}`, {params, headers: this.headers});
  }
}