http.post给出TypeError:“循环对象值”

时间:2018-07-29 10:28:31

标签: angular laravel

我正在使用Angular CLI和PHP Laravel。我尝试通过http.post将表单数据发送到我的控制器。如果我这样定义主体,它会起作用:

{
email: 'test@something.com',
password: 'thepassword' 
}

但是,如果我使用表格中的字段,则会得到TypeError: "cyclic object value"

import {Component} from '@angular/core';
import {FormControl, FormGroup, Validators} from '@angular/forms';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-registration',
  templateUrl: './registration.component.html'
})

export class RegistrationComponent {
    registrationForm = new FormGroup({
        txtRegEmail: new FormControl('',
            Validators.compose([
                Validators.required
            ])
        ),
        txtRegPassword: new FormControl('', Validators.compose([
            Validators.required,
            Validators.minLength(8)
        ])),
        txtRegPassword2: new FormControl('', Validators.compose([
            Validators.required,
            Validators.minLength(8)
        ]))
    });
    get txtRegEmail() { return this.registrationForm.get('txtRegEmail'); }
    get txtRegPassword() { return this.registrationForm.get('txtRegPassword'); }
    get txtRegPassword2() { return this.registrationForm.get('txtRegPassword2'); }

    constructor(private http: HttpClient) { }

    regUrl = 'backend/public/test';

    sendRegistration() {
        this.http.post(this.regUrl, {
            email : this.txtRegEmail,
            password: this.txtRegPassword
        }).subscribe(
            res => {
                console.log(res);
            }, error1 => {
                console.log(error1);
            }
        );
    }
}

2 个答案:

答案 0 :(得分:0)

尝试:

get txtRegEmail() { return this.registrationForm.get('txtRegEmail').value }

答案 1 :(得分:0)

   sendRegistration() {
    this.http.post(this.regUrl, {
        email : this.txtRegEmail.value,
        password: this.txtRegPassword.value
    }).subscribe(
        res => {
            console.log(res);
        }, error1 => {
            console.log(error1);
        }
    );
}