Error with Angular 2 to create Post and HTTP response

0

Because when creating a Post, the json returns by default, I have my service.ts

addRestaurante(restaurante: Restaurante) {
    let json = JSON.stringify(restaurante);
        let params = "json="+json;
        let headers = new Headers({'Content-Type':'application/x-www-form-urlencoded'});
        return this._http.post(this.URL_API, params, {headers: headers}).map(res => res.json());
    }

and my component create-restaurant

import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import {Restaurante} from '../model/restaurante';
import {RestauranteServicio} from '../services/restaurantes.services';
@Component({
  selector: 'app-agregarestaurante',
  templateUrl: './agregarestaurante.component.html',
  styleUrls: ['./agregarestaurante.component.css'],
  providers: [RestauranteServicio]
})
export class AgregaRestauranteComponent implements OnInit {
  public titulo = 'Crea un Restaurante';
  public restaurante: Restaurante;
  public status: string;
  public unerror: string;
  constructor(
    private _restauranteServicio : RestauranteServicio,
    private route : ActivatedRoute,
    private router : Router
  ) { }
  ngOnInit() {
    this.restaurante = new Restaurante(0,"","","","null","bajo");
  }
  onSubmit() {
   this._restauranteServicio.addRestaurante(this.restaurante).subscribe(
  response => { 
    this.status = response.status;
    this.restaurante = response.data;
    if(this.status !== "success"){
      alert('error on server');
    }
    },
    error => {
      this.unerror = <any>error;
      if(this.unerror !== null) {
        console.log(this.unerror);
        alert('error en la peticion');
      }
    }
    );
    this.router.navigate(["/"]);
  }
}

When submitting I get "error on the server" which could be my error repo

    
asked by Gerardo Bautista 16.10.2018 в 01:10
source

0 answers