在Angular 2中发布数据

时间:2018-08-21 03:52:34

标签: node.js angular

我想将数据发布到Angular的Web API中。我正在使用以下代码,但无法解决此问题

register() {
    const httpHeaders = new HttpHeaders()
        .set('Accept', 'application/json');
    const httpParams = new HttpParams()

        .set('name', this.name.value)
        .set('username', this.username.value)
        .set('email', this.email.value)
        .set('password', this.password.value);


    this.http.post('api', {
        headers: httpHeaders,
        params: httpParams,
        responseType: 'json'
    }).subscribe(data => {

        alert('Register Success!');

      } error =>{


    });
  }

这是注册表格,请帮助我

1 个答案:

答案 0 :(得分:0)

请看这里 有关如何在发布请求中传递数据的详细说明(角度2)。

Angular post request demo

testRequest() {
  var body = 'username=myusername&password=mypassword';
  var headers = new Headers();
  headers.append('Content-Type', 'application/x-www-form-urlencoded');

  this.http
    .post('/api',
      body, {
        headers: headers
      })
      .subscribe(data => {
            alert('ok');
      }, error => {
          console.log(JSON.stringify(error.json()));
      });

}

相关问题