I am developing my application with Nodejs
, angular-cli
.
I was configuring the server in one of its routes, where I make a request of type POST
, where the server returns some data and a token , but I have a problem when I make a request from the client (angular-cli) , and that is that it returns two answers to me as seen in the following image:
Now, when I make the request from postman and everything works as I hope it works and returns the data I want, here is the image:
As I mentioned before, I do not know why when I make the request from the client in angular
, it generates two responses on the server.
Here I attach the code developed in angular:
import { Component, OnInit } from '@angular/core';
import {LoginService} from'./login.service';
import { EmailValidator } from '@angular/forms';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
//usuario:String;
//contraseña:String;
re = new RegExp('/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/');
constructor(private logserv : LoginService) {
}
ngOnInit() {
}
ingresar(usuario , contrasenia){
alert(' se iniciara sesion con el usuario : ' + usuario)
return this.logserv.iniciarSesion(usuario,contrasenia)
.subscribe((res:any)=>{console.log(res)});
//}
}
}
In this part I attach the service that is responsible for making the post request:
import { Injectable } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { HttpClient, HttpHeaders} from '@angular/common/http';
import { Observable} from 'rxjs/Observable';
import { Response, Headers } from '@angular/http';
import { RequestOptions } from '@angular/http';
import 'rxjs/Rx';
@Injectable()
export class LoginService {
constructor(private http:HttpClient) { }
iniciarSesion(usuariolog : String, contraseñalog: String){
this.http.post('http://url/login/',{"usuario":usuariolog,"contrasena":contraseñalog});
}
}