角度发布请求将数据发送到服务器

时间:2019-10-02 14:20:45

标签: angular http header

我正在尝试将数据发送到服务器,但只是没有到达那里

 signUp(email: string, password: string) {
    let data = { email, password };
    let headers = new HttpHeaders({
      'Content-Type':'application/json'
    });

    console.log('Requesting', email, password);
    return this.http.post(this.sellerApi, data, { responseType: 'text', headers });
  }

  this.api.signUp(email, password).subscribe( r => console.log(r));

我在服务器上打印了请求,但请求为空

2 个答案:

答案 0 :(得分:1)

角度http.post从服务器返回结果的Observable。您必须使用目标函数订阅可观察对象,以便对结果(以及任何错误)进行处理。

答案 1 :(得分:0)

首先,您需要在import HttpClientModule from '@angular/common/http'中插入app.module.ts。然后,您需要在组件constructor(DI)中注入httpClient依赖项,如下所示:

  constructor(private http: HttpClient) {
  }

  signUp(email: string, password: string) {
    let data = { email, password };
    let headers = new HttpHeaders({
      'Content-Type': 'application/json'
    });

    console.log('Requesting', email, password);
    return this.http.post(this.sellerApi, data, { responseType: 'text', headers : headers });
  }

  testApi() {
    this.signUp(email, password).subscribe(r => console.log(r));
  }