Angular 2在POST请求

时间:2016-08-11 20:35:48

标签: angular

我正在将应用程序从Angular 2 Beta 9迁移到Angular 2 RC5,我遇到了大量的错误,我开始消除这些错误,并且我的控制台没有显示任何错误。

然而,当我向rails服务器提交表单时,页面只是重新加载,控制台很快显示错误,但我看不到它太快

无论如何,下面的代码在测试版上运行得很好,所以对这里出错的任何见解都会很棒(只发布相关部分)

onSubmit() {

    this.myForm.value.systemParameters = this.system_arr;
    this.myForm.value.liftOperator = this.lift_arr;
    this.myForm.value.restrictOperator = this.restrict_arr;
    this.myForm.value.param = this.param_arr;
    this.myForm.value.xInitial = this.xinitial_arr;

    let myForm = JSON.stringify(this.myForm);

    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    this.http.post('http://localhost:3000/webhook', myForm, { headers: headers }).subscribe(
      res => this.response = res.json(),
      error => console.log(error)
    );

    console.log('your submitted value:', this.myForm.value);
  }

2 个答案:

答案 0 :(得分:2)

我不确定我是否正确理解您的问题,但如果问题是每次POST请求后页面重新加载,我建议将提交按钮的类型更改为“按钮”。这将阻止页面在向服务器发送POST请求时重新加载。

喜欢这个家伙建议https://stackoverflow.com/a/33304509/4332884

答案 1 :(得分:0)

我的问题是带有提交类型的按钮元素。删除提交并将按钮元素更改为anhor元素。那应该可以解决问题。

错误:

<form>
...
<button type="submit" (click)="letsDoThis()">Submit/button>
</form>

右:

<form>
...
<a (click)="letsDoThis()">Submit</a>
</form>