1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

"Http failure during parsing" in Angular

Discussão em 'Angular' iniciado por user14536811, Novembro 7, 2024 às 11:32.

  1. user14536811

    user14536811 Guest

    I am trying to connect my Frontend in Angular to an Api developed using .NetCore but I am getting the following error

    Http failure during parsing for http://localhost:5000/User/register


    Despite the error , the users are still being saved to the database. Below are my rest service, my component ts and the register method in the backend

    Rest Service


    const endpoint = 'http://localhost:5000/';
    const httpOptions = {
    headers: new HttpHeaders({
    'Content-Type': 'application/json',
    }),
    };

    @Injectable({
    providedIn: 'root'
    })
    export class RestService {

    constructor(private http: HttpClient) { }

    private extractData(res: Response) {
    let body = res;
    return body || {};
    }

    addUser(user: User): Observable<User> {
    return this.http.post<User>(
    endpoint + 'User/register',
    JSON.stringify(user),
    httpOptions
    );
    }
    }

    Register Component .ts


    @Component({
    selector: 'app-register',
    templateUrl: './register.component.html',
    styleUrls: ['./register.component.css']
    })
    export class RegisterComponent implements OnInit {
    @Input() userData: User = new User();

    constructor( public rest: RestService,
    private route: ActivatedRoute,
    private router: Router) { }

    ngOnInit(): void {
    }

    addUser() {
    this.rest.addUser(this.userData).subscribe(
    (result:any) => {
    console.log(result);
    this.router.navigate(['/login']);
    },
    (err) => {
    console.log(err);
    console.log("DADOS"+JSON.stringify(this.userData));
    }
    );
    }
    }

    Method in the backend


    [AllowAnonymous]
    [HttpPost("register")]
    public IActionResult Register([FromBody]RegisterModel model)
    {
    var user = _mapper.Map<User>(model);

    try
    {
    // create user
    _userService.Create(user, model.Password);
    return Ok("Successful registration");
    }
    catch (AppException ex)
    {
    // return error message if there was an exception
    return BadRequest(new { message = ex.Message });
    }
    }

    Continue reading...

Compartilhe esta Página