簡體   English   中英

httpClient獲取請求獲取404找不到響應

[英]httpClient get request getting 404 not found response

在將一些使用http的api調用更改為httpClient之后,我將項目版本從5.1版角遷移到了8,但是一旦我們啟動該項目,對服務層的第一個調用就找不到404,該調用是沒有打服務層,下面是我的代碼

 const routes: Routes = [ { path: '', component: MasterComponent, resolve: { "appUser": CheckAccessService }, children: [ { matcher: ReadFinder, loadChildren: () => import('app/Read-Finder/read-finder.module').then(m => m.ReadFinderModule), canLoad: [CheckAccessService], canActivateChild: [CheckAccessService] }, ---------------- -------------- ] }, { path: '', component: MasterPageComponent, resolve: { "appUser": CheckAccessService }, children: [ { path: 'NoAuthorityPage', component: NoAuthorityPageComponent }, { path: '**', component: PageNotFound }, ] }, ]; 

在CheckAccessService中,用於解析的代碼如下所示,

 @Injectable() export class CheckAccessService implements Resolve<UserInfo>, CanLoad, CanActivate, CanActivateChild { resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<UserInfo> { let url = window.location.pathname; let _url = url.startsWith('/') ? url.substring(1) : url; return new Observable<UserInfo>((subscriber: Subscriber<UserInfo>) => { if (this.userInfo) { subscriber.next(this.getCachedUserInfo()); subscriber.complete(); return; } this.navApiService.getUserInfo() .subscribe( user => { this.storeUserInfo(user); setTimeout(() => { subscriber.next(user); subscriber.complete(); }); }, err => { this.storeUserInfo(null); //clear session storage for unlogged users window.sessionStorage.clear(); setTimeout(() => { this.redirectOnFail(url, err.status === 403); }, 3000); } ) }); } } 

在resolve方法中,我們有一個api調用,這是從應用程序到服務器的第一個調用,看起來像這樣,

 import { HttpClient } from '@angular/common/http'; @Injectable() export class ApiService { constructor(public http: HttpClient) { } getUserInfo(): Observable<UserInfoData> { return this.http.get('/api/security/user-info') .map(resp => <UserInfoData>resp) .catch(err => { return observableThrowError(err); }); } } 

chrome中的錯誤看起來像這樣,

在此處輸入圖片說明

朋友!

首先,我擔心我的英語,在將Angular版本更新為8時,我遇到了同樣的問題,我使用的是http,但是版本8已過時,必須遷移所有服務。到HttpClienr。 嘗試將您的angular版本更新為版本7。 問候!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM