angular 5在POST请求中出错。发送标题参数不起作用

时间:2018-05-09 05:41:13

标签: angular angular5 angular-http

我正在尝试发送一个POST请求,如:

public getAppData(countryPath) : Observable<any> {
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    let options = new RequestOptions({ headers: headers });
    return this.http.post(this.retailApi, JSON.stringify(this.postParam), {headers: options } );
}

但我收到的错误是:

Option not works

这里有什么问题?谁能帮我解决一下呢?

在我尝试使用其他选项后,我收到了此错误:

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个:

请像这样导入HttpClientHttpHeaders

import { HttpClient, HttpHeaders } from '@angular/common/http';

并注入你的班级:

constructor(private http: HttpClient) {
}

和你的方法是这样的:

 public getAppData(countryPath):Observable<any>{
      let headers: HttpHeaders = new HttpHeaders();
      headers.set('Content-Type', 'application/json');
      return this.http.post(this.retailApi, JSON.stringify(this.postParam), { headers: headers });
    }
相关问题