提问人:GABRIEL DE AZEVEDO CAMARGO 提问时间:11/18/2023 最后编辑:Brian Tompsett - 汤莱恩GABRIEL DE AZEVEDO CAMARGO 更新时间:11/18/2023 访问量:21
在 Nodejs 中使用 angular 向我的 API 发出请求时出错 [已关闭]
Error in making request using angular to my API in Nodejs [closed]
问:
我正在尝试在 angular 版本 17 中独立发出一个简单的请求,但我只是收到错误,我的代码如下所示:
config.ts:
// app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
import { provideClientHydration } from '@angular/platform-browser';
import { HttpBackend, HttpClient } from '@angular/common/http';
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
provideClientHydration(),
{
provide: HttpClient,
useFactory: (backend: HttpBackend) => {
return new HttpClient(backend);
},
},
],
};
login.component.ts:
// login.component.ts
import { Component } from '@angular/core';
import { AuthService } from '../auth.service';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
@Component({
selector: 'app-login',
standalone: true,
imports: [FormsModule, HttpClientModule],
templateUrl: './login.component.html',
styleUrls: ['./login.component.sass']
})
export class LoginComponent {
loginEmail: string = '';
loginSenha: string = '';
registerNome: string = '';
registerEmail: string = '';
registerSenha: string = '';
constructor(private authService: AuthService) {}
login(): void {
this.authService.login(this.loginEmail, this.loginSenha)
.subscribe(
response => {
localStorage.setItem('token', response.token);
console.log("dados recebidos");
// Faça o que precisa após o login, por exemplo, redirecione para outra página
},
error => {
console.log("erro no login");
}
);
}
register(): void {
this.authService.register(this.registerNome, this.registerEmail, this.registerSenha).subscribe(
response => {
localStorage.setItem('token', response.token);
// Faça o que precisa após o registro, por exemplo, redirecione para outra página
},
error => {
console.error('Erro no cadastro:', error);
// Trate o erro, exiba uma mensagem ao usuário, etc.
}
);
}
}
auth.service.ts:
// auth.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { tap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class AuthService {
private baseUrl = 'https://pesquisa-lymphoma.vercel.app/api';
constructor(private http: HttpClient) {}
login(email: string, senha: string): Observable<any> {
const body = { email, senha };
console.log('URL de login:', `${this.baseUrl}/getTestData?email=${body.email}&senha=${body.senha}`);
const url = `${this.baseUrl}/getTestData?email=${body.email}&senha=${body.senha}`;
this.http.get(url).pipe(
tap(data => {
console.log('Dados obtidos:', data);
// Faça qualquer manipulação adicional nos dados, se necessário
})
);
return this.http.get(`${this.baseUrl}/getTestData?email=${body.email}&senha=${body.senha}`)
}
register(nome: string, email: string, senha: string): Observable<any> {
const body = { nome, email, senha };
console.log(body);
return this.http.post(`${this.baseUrl}/register`, body).pipe(
catchError(this.handleError)
);
}
private handleError(error: any): Observable<never> {
console.error('Erro na requisição:', error);
return throwError(error);
}
}
例如,登录表单将数据发送到 Auth 没有任何问题,但这可能是请求的问题(只是我的猜测)。 我希望能够发出登录请求,因为其余部分的登录请求更像是一样的
答: 暂无答案
评论