以角度发送发帖请求不起作用

时间:2019-09-27 22:26:37

标签: javascript angular typescript

我正尝试在上角发送一个发帖请求,并且我很确定问题不在后端,因为我使用了邮递员,并且可以在邮递员上工作。

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

@Injectable()
export class ConHttpService {
  private headers: HttpHeaders;
  private accessPointUrl: string = 'https://localhost:44340/api/contact';

  constructor(private http: HttpClient) {
    this.headers = new HttpHeaders({'Content-Type': 'application/json; charset=utf-8'});
  }


  public addcon(payload) {
    console.log("hi") //prints hi
   console.log(this.accessPointUrl, payload, {headers: this.headers}) //gives a link that take me to a url full of json of targeted database but without my added object
    return this.http.post(this.accessPointUrl, payload, {headers: this.headers});
  }
}

在发送组件中:

  onSubmit(){
    console.log(this.contactForm.value)
    this.conhttp.addcon(this.contactForm.value)
  }

1 个答案:

答案 0 :(得分:1)

HttpClient.post()返回一个可观察值。为了调用该操作,您需要在该可观察对象上调用subscribe()

this.conhttp
  .addcon(this.contactForm.value)
  .subscribe()