Angular - How to send an object inside a model in a POST method?

Asked

Viewed 181 times

0

Good afternoon, I’m having a problem sending a POST to a published API on account of one of the expected parameters being an object, so the expected json is as follows :

{"Cargo": string,
 "idFuncionario": 0,
 "idCadastro": 0,
  "cadastro": {
                "idCadastro": 0,
                "nome": "string",
                 "razao": "string",
                  "documento": "string"
              }

"dtAtualizacao": "2019-02-22T17:32:38.049Z",
}

the only value that arrives null in the API is that of the object "registration", I tried in several ways to send it, unsuccessfully, since I am starting at the angle now, any help would be of extreme benefit, the method post in service is as follows:

    postFuncionario(funcionario : Funcionario){   

      try {
          var body = JSON.stringify(funcionario);
            var headerOptions = new Headers({'Content-Type':'application/json'});
            var requestOptions = new RequestOptions({method : RequestMethod.Post,headers : headerOptions});
            return this.http.post(this.localUrl+'Create'
            ,body,
            requestOptions).map(x => x.json());              
        }

      catch (error) {
            return Observable.throw(error)
          }

    }    

the onsubmit method , as a component:

 onSubmit(form:NgForm){


if (form.value.idFuncionario   == null) {
  this.service.postFuncionario(this.form.value)
    .subscribe(data => {
      this.toastr.success('Novo registro gravado com sucesso!', 'Funcionário Cadastrado')
      console.log(this.service.funcionario )
      console.log(this.form.value)
      console.log(form.value.idFuncionario)
      this.resetForm()
    })
}

example of databinding in template:

      <input type="number" class="form-control" name="idFuncionario"  placeholder="ID Funcionário"
                 [(ngModel)]="service.funcionario.idFuncionario" value="{{service.funcionario?.idFuncionario}}">

      <input type="text" class="form-control" name="nome" placeholder="Nome" #nome="ngModel"
                [(ngModel)]="service.funcionario.cadastro.nome" value="{{service.funcionario?.cadastro.nome}}"> 

finally, the model:

 import {Cadastro} from './cadastro.model'


 export class Funcionario {

 Cargo : string
 idFuncionario : number
 cadastro : Cadastro
 }

any help would be great!

  • in the case, I am using angular 7 with all updated Packages

1 answer

0

in case is missing a comma:

{"Cargo": string,
 "idFuncionario": 0,
 "idCadastro": 0,
  "cadastro": {
                "idCadastro": 0,
                "nome": "string",
                 "razao": "string",
                  "documento": "string"
              }
 aqui ,
"dtAtualizacao": "2019-02-22T17:32:38.049Z",
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.