我有一个MEAN应用。
Angular CLI:7.1.4
Node :10.1.0
操作系统:win32 x64
Angular :7.1.4
最近,来自HttpClientModule的http请求被卡住,无法发布到 Node 服务器:
Img of chrome dev tools xhr pending request
nodejs服务器(在本地和生产环境中运行(Azure Web应用程序)并不表示它曾经收到过该请求。这不一致地发生。有时完成,有时挂起。
这是从Angular到服务器的testConnection调用的片段:
Angular 服务
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { environment } from '../../environments/environment';
const Headers: any = { withCredentials: true, responseType: 'json', headers: { 'Content-Type': 'application/json' } };
@Injectable({
providedIn: 'root',
})
export class UserService {
constructor(private _http: HttpClient) {}
loginStatus() {
return this._http.get(`${environment.serverURL}/api/login-status`, Headers).pipe(catchError(this.handleError));
}}
Angular 分量:
ngOnInit() {
this._userSvc.loginStatus().subscribe(
(result:any)=>{console.log(result)},
(error:any)=>{console.log(error)})
}
Node/表达:
router.get('/login-status', (req, res, next) => {
if (req.isAuthenticated()) {
res.status(200).json(req.user);
} else {
res.status(403).json({
success: false,
error: 'User not Authenticated',
message: "Please return to the login in page and try again."
})
}
})
Node 正在使用护照进行身份验证
不要因为护照问题而束手无策,因为这并不总是失败的。我有一些简单的路由,它们不进行验证,只是返回一些失败的文本。
我尝试修改我的CORS选项,但只成功阻止了自己。
有时重新启动服务器会允许请求完成,但并非总是如此。
最佳答案
我发现了问题,我很尴尬地说这是mssql的SQL连接字符串中的问题。
我有((config,error)=> {})而不是正确的(config,(err)=> {});这是在护照的反序列化用户功能中。没有什么比查看数千行代码来发现一个小问题更重要的了。
关于node.js - Angular 2+ http.get()处于待处理状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54894170/