typescript - .Subscribe() 函数接收值但无法进入函数块
问题描述
使用 rxjs observable 从前端调用 springboot 后端的服务。即使使用 .subscribe 函数调用此服务,它已成功返回一个值,我也无法进入订阅块,它直接到达块的末尾。
登录组件.ts
validate() {
console.log('need to validate');
this.loginService.checkLogin(this.userName, this.pwd).subscribe(resp => {
console.log(resp);
if (resp === 'success') {
this.router.navigateByUrl('/home');
}
}, error => {
this.toastr.error(error.error.message);
});
}
登录服务.ts
export class LoginServiceService {
url = 'http://localhost:8089/login/check';
constructor( private http: HttpClient) { }
public checkLogin(userName, password): Observable <any> {
let headersValue = new HttpHeaders();
headersValue = headersValue.set('Access-Control-Allow-Origin', '*');
headersValue = headersValue.set('Content-Type', 'application/json');
headersValue = headersValue.set('userName', userName);
headersValue = headersValue.set('password', password);
return this.http.get<any[]>(this.url, { headers: headersValue });
}
}
预期结果:它应该进入订阅块并在控制台中显示 resp 并输入 if 块。
实际结果:只显示“需要验证”。
并在错误检查后直接到达块的末尾。
解决方案
我认为您的问题可能与标题有关。我不确定您是否需要其中任何一个,但更具体地说,我认为用户名和密码根本不属于那里。他们可能应该是参数。我认为以下内容可能会对您有所帮助:
public checkLogin(userName, password): Observable <any> {
let headersValue = new HttpHeaders();
headersValue = headersValue.set('Access-Control-Allow-Origin', '*');
headersValue = headersValue.set('Content-Type', 'application/json');
const params = { userName, password };
return this.http.get<any[]>(this.url, { headers: headersValue, params });
}
推荐阅读
- python - 带有 argon2_cffi 的哈希密码
- python - Django 2 request.POST 未使用 instance=request.user 保存到数据库
- python - 代理无法使用 firefox geckodriver 处理 python selenium
- python - 如何过滤熊猫数据框中包含空列表的行?
- node.js - NodeJS 和 AWS Lambda 的异步等待问题
- python - CatBoost 边框
- c# - 带有 SteamVR 的 Meta 2;无法在 Unity 中移动(几乎)冻结
- ios - 当我们使用两个不同的 xcode 版本创建 IPA 文件时,ipa 文件有什么不同吗?
- wifi - 如何使用cfg80211->start_ap?
- c# - 如何延迟服务窗口中的 OnStop 方法?