我正在使用角度8
错误
HttpErrorResponse {标头:HttpHeaders,状态:502,statusText:“错误 网关”,网址: “ http://central-portal-app-alb-1565375077.ap-south-1.elb.amazonaws.com/api/v1/user/login”, 好的:false,…}标头:HttpHeaders {normalizedNames:Map(0), lazyUpdate:null,lazyInit:ƒ}状态:502 statusText:“错误的网关”
这是我的服务文件代码
service.ts
@Injectable()
export class RestService {
baseUrl = environment.baseUrl;
login = this.baseUrl + 'user/login';
constructor(private http: HttpClient) {
}
userlogin(userid, smpassword) {
const params = new HttpParams()
.set('user_id', userid)
.set('sm_password', smpassword);
console.log(params);
return this.http.post(this.baseUrl + 'user/login', params.toString(),
{
headers: new HttpHeaders({
'Content-Type' : 'application/json'
})
}
);
login.component.ts
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
Userlogin: FormGroup;
constructor(private route: Router , private fb: FormBuilder , private rest: RestService) {
this.Userlogin = this.fb.group({
email : ['', [Validators.required , Validators.pattern('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$') ]],
password : ['', [Validators.required , Validators.minLength(5) ] ]
});
}
// , Validators.required
ngOnInit() { }
login() {
const email = this.Userlogin.get('email').value;
const password = this.Userlogin.get('password').value;
this.rest.userlogin(email, password).subscribe(
result => {
console.log(result);
}
);
// this.route.navigateByUrl('/pendingapproval');
}
答案 0 :(得分:0)
尝试访问浏览器上的api端点,检查URL是否正确,并且可能在标头中包含访问控制允许源
答案 1 :(得分:0)
不确定,但是请检查一下……
这应该是您的服务文件代码
userlogin(userid, smpassword) {
const params = new HttpParams()
.set('user_id', userid)
.set('sm_password', smpassword);
console.log(params);
return this.http.post(this.baseUrl+'user/login',JSON.stringify(params)).pipe(map((res) => {
return res;
}));
}
这应该是您的 login.component.ts
login() {
const email = this.Userlogin.get('email').value;
const password = this.Userlogin.get('password').value;
this.rest.userlogin(email, password).subscribe((result)=> {
result = JSON.Parse(result)
console.log(result);
}
);