Javascript 在Angular 8中调用api,未定义结果
我有一个angular 8应用程序,希望调用Get api调用 但我得到了这个错误:Javascript 在Angular 8中调用api,未定义结果,javascript,angular,api,http,angular-http-interceptors,Javascript,Angular,Api,Http,Angular Http Interceptors,我有一个angular 8应用程序,希望调用Get api调用 但我得到了这个错误: stack: "ReferenceError: result is not defined↵ at eval (eval at push../src/app/dossier/dossier-physical/dossier-physical.component.ts.DossierPhysicalComponent.ngOnInit @Injectable({ providedIn: 'roo
stack: "ReferenceError: result is not defined↵ at eval (eval at push../src/app/dossier/dossier-physical/dossier-physical.component.ts.DossierPhysicalComponent.ngOnInit
@Injectable({
providedIn: 'root'
})
export class HttpMaintenanceInterceptor implements HttpInterceptor {
constructor(private auth: AuthService ) {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${this.auth.getAccessToken()}`
}
});
return next.handle(request);
}
}
这是ts文件:
export class DossierPhysicalComponent implements OnInit {
entries: Array<DossierEntry> = [];
single: DossierEntry;
showSingle: boolean;
dossiersLoaded = false;
constructor(
private healthAPIService: HealthAPIService,
private documentCorrespondencService: DocumentCorrespondenceService
) {}
ngOnInit() {
this.healthAPIService.getDossierEntry('physical').subscribe(result => {
console.log(result.values);
this.entries = result;
this.dossiersLoaded = true;
});
}
}
这是:
stack: "ReferenceError: result is not defined↵ at eval (eval at push../src/app/dossier/dossier-physical/dossier-physical.component.ts.DossierPhysicalComponent.ngOnInit (http://localhost:4200/dossier-dossier-module-ngfactory.js:18184:14), <anonymous>:1:1)↵ at DossierPhysicalComponent.push../src/app/dossier/dossier-physical/dossier-physical.component.ts.DossierPhysicalComponent.ngOnInit (http://localhost:4200/dossier-dossier-module-ngfactory.js:18184:14)↵ at checkAndUpdateDirectiveInline (http://localhost:4200/vendor.js:37850:19)↵ at checkAndUpdateNodeInline (http://localhost:4200/vendor.js:46063:20)↵ at checkAndUpdateNode (http://localhost:4200/vendor.js:46025:16)↵ at debugCheckAndUpdateNode (http://localhost:4200/vendor.js:46659:38)↵ at debugCheckDirectivesFn (http://localhost:4200/vendor.js:46619:13)↵ at Object.updateDirectives (http://localhost:4200/dossier-dossier-module-ngfactory.js:18152:711)↵ at Object.debugUpdateDirectives [as updateDirectives] (http://localhost:4200/vendor.js:46611:21)↵ at checkAndUpdateView (http://localhost:4200/vendor.js:46007:14)"
getDossierEntry( patientUUID: string, type: String = '' ): Observable<DossierEntry[]> {
patientUUID = '0d584905-fc20-4723-870a-0f0214419507';
const entryType = type === '' ? 'all' : 'type/' + type;
console.log(this.http.get<DossierEntry[]>('/api/patient/' + patientUUID + '/DossierEntry/' + entryType ));
console.log(patientUUID);
return this.http.get<DossierEntry[]>('/api/patient/' + patientUUID + '/DossierEntry/' + entryType );
}
我得到这个输出:
return this.http.get<DossierEntry[]>('/api/patient/{patientUUID}/DossierEntry/' + entryType
HttpErrorResponse{headers:HttpHeaders,状态:404,状态文本:“未找到”,url:http://localhost:4200/api/patient/$%7bPatientUID%7D/DossierEntry/all“,确定:false,…}
错误:“↵↵↵↵错误↵↵↵无法获取/api/patient/$%7patientuuid%7D/DossierEntry/all↵↵↵"
Header:HttpHeaders{normalizedNames:Map(0),lazyUpdate:null,lazyInit:ƒ}
消息:“的Http失败响应”http://localhost:4200/api/patient/$%7b未找到PatientuUID%7D/DossierEntry/all:404”
名称:“HttpErrorResponse”
好:错
现状:404
statusText:“未找到”
url:“http://localhost:4200/api/patient/$%7bPatientUID%7D/档案/全部”
__原型:HttpResponseBase
即使我这样做:
ngOnInit() {
this.healthAPIService.getDossierEntry('physical').subscribe((result:any)=> {
console.log(result.values);
this.entries = result;
this.dossiersLoaded = true;
});
}
getDossierEntry(patientUUID:string,type:string=''):可观察{
patientUUID='0d584905-fc20-4723-870a-0f0214419507';
const entryType=type=='''all':'type/'+type;
log(this.http.get('/api/patient/'+patientUUID+'/DossierEntry/'+entryType));
console.log(patientUUID);
返回此.http.get('/api/patient/'+patientUUID+'/DossierEntry/'+entryType);
}
不起作用,因为我看到您得到的响应是404,这意味着找不到URL 在代码中 您没有替换可以在URL中检查的
patientUUID
的值
getDossierEntry( patientUUID: string, type: String = '' ): Observable<DossierEntry[]> {
const entryType = type === '' ? 'all' : 'type/' + type;
return this.http.get<DossierEntry[]>('/api/patient/' + patientUUID + '/DossierEntry/' + entryType );
}
返回this.http.get('/api/patient/{patientUUID}/DossierEntry/'+entryType
在开发者模式下,您应该添加proxy.conf.json转发到服务器:
例如:
getDossierEntry( patientUUID: string, type: String = '' ): Observable<DossierEntry[]> {
const entryType = type === '' ? 'all' : 'type/' + type;
return this.http.get<DossierEntry[]>('/api/patient/${patientUUID}/DossierEntry/' + entryType);
}
将结果类型映射到任意或
console.log(this.http.get<DossierEntry[]>('/api/patient/' + patientUUID + '/DossierEntry/' + entryType)
或
要获得预期结果,请使用下面的选项,使用
patientUUID
选项1:
问题:patientUUID
值未附加到目标位置的URL,并且找不到404,因为它被证明是无效的
要解决此问题,请使用参数更新上述API url-patientUUID和键入,并在返回检查url之前调试添加控制台,如下所示
console.log(this.http.get('/api/patient/'+patientUUID+'/DossierEntry/'+entryType)
您正在调用一个返回404错误的URL。可能您在URL中对patientUUID的处理不正确。在进行api调用时,您似乎没有替换patientUUID
值。但是,如果是错误,订阅者为什么会得到未定义的值?Thrower(“您不会看到此消息”)。订阅(console.log)不打印任何东西。中间有一个拦截器,占用HTTP错误响应并发出空值。更新POK OK,将是Nexee POST UPDATE。不理解这一点。1337是什么?我更新POSITT只是一个例子。我的服务器正在监听端口1337。如果您调用相对URL(/API)你应该将它重定向到你的服务器,如果你用ip调用外部,你应该设置绝对url,因为相对调用返回到运行angular的服务器。OK,但这不是问题。我更新了post,但是我必须更改什么?你能说我做错了什么吗?或者我必须发布更多代码吗?如果我添加了拦截器,那就更好了。但是那么我必须更改什么?谢谢,但是如何在URL中发出patientUUID呢?谢谢谢谢你的帮助。但是仍然会出现错误。更新后请将结果放入括号中,如**(结果:任意)=>
**并检查一次。但这与调用方法无关吗?因为我当然不会触发该方法,当我使用httpClientatSantosh时,您还有其他建议吗?我添加了拦截器。谢谢。我明天会检查它,现在就让您检查。但是我可以将文件发送给您吗?@mightycodeentastantosh,我认为不允许发送g文件,但是你可以创建stackblitz来复制你的问题,但是我们可以去聊天。但是你可以邀请我聊天吗?因为我看不到这里的功能。Naga,你在吗?我正在测试它
core.js:7376 ERROR
HttpErrorResponse {headers: HttpHeaders, status: 404, statusText: "Not Found", url: "http://localhost:4200/api/patient/%7BpatientUUID%7D/DossierEntry/type/physical", ok: false, …}
error: "<!DOCTYPE html>↵<html lang="en">↵<head>↵<meta charset="utf-8">↵<title>Error</title>↵</head>↵<body>↵<pre>Cannot GET /api/patient/%7BpatientUUID%7D/DossierEntry/type/physical</pre>↵</body>↵</html>↵"
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure response for http://localhost:4200/api/patient/%7BpatientUUID%7D/DossierEntry/type/physical: 404 Not Found"
name: "HttpErrorResponse"
ok: false
status: 404
statusText: "Not Found"
url: "http://localhost:4200/api/patient/%7BpatientUUID%7D/DossierEntry/type/physical"
__proto__: HttpResponseBase
stack: "ReferenceError: result is not defined↵ at eval (eval at push../src/app/dossier/dossier-physical/dossier-physical.component.ts.DossierPhysicalComponent.ngOnInit (http://localhost:4200/dossier-dossier-module-ngfactory.js:18184:14), <anonymous>:1:1)↵ at DossierPhysicalComponent.push../src/app/dossier/dossier-physical/dossier-physical.component.ts.DossierPhysicalComponent.ngOnInit (http://localhost:4200/dossier-dossier-module-ngfactory.js:18184:14)↵ at checkAndUpdateDirectiveInline (http://localhost:4200/vendor.js:37850:19)↵ at checkAndUpdateNodeInline (http://localhost:4200/vendor.js:46063:20)↵ at checkAndUpdateNode (http://localhost:4200/vendor.js:46025:16)↵ at debugCheckAndUpdateNode (http://localhost:4200/vendor.js:46659:38)↵ at debugCheckDirectivesFn (http://localhost:4200/vendor.js:46619:13)↵ at Object.updateDirectives (http://localhost:4200/dossier-dossier-module-ngfactory.js:18152:711)↵ at Object.debugUpdateDirectives [as updateDirectives] (http://localhost:4200/vendor.js:46611:21)↵ at checkAndUpdateView (http://localhost:4200/vendor.js:46007:14)"
@Injectable({
providedIn: 'root'
})
export class HttpMaintenanceInterceptor implements HttpInterceptor {
constructor(private auth: AuthService ) {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${this.auth.getAccessToken()}`
}
});
return next.handle(request);
}
}
getDossierEntry( patientUUID: string, type: String = '' ): Observable<DossierEntry[]> {
const entryType = type === '' ? 'all' : 'type/' + type;
console.log(this.http.get<DossierEntry[]>('/api/patient/' + patientUUID + '/DossierEntry/' + entryType));
return this.http.get<DossierEntry[]>('/api/patient/${patientUUID}/DossierEntry/' + entryType);
}
ngOnInit() {
this.documentCorrespondencService.getDossierEntry('physical').subscribe((result: any)=> {
console.log(result.values);
this.entries = result;
this.dossiersLoaded = true;
});
}
Observable {_isScalar: false, source: Observable, operator: MapOperator}
operator: MapOperator
project: ƒ (res)
thisArg: undefined
__proto__: Object
source: Observable {_isScalar: false, source: Observable, operator: FilterOperator}
_isScalar: false
__proto__: Object
HttpErrorResponse {headers: HttpHeaders, status: 404, statusText: "Not Found", url: "http://localhost:4200/api/patient/$%7BpatientUUID%7D/DossierEntry/all", ok: false, …}
error: "<!DOCTYPE html>↵<html lang="en">↵<head>↵<meta charset="utf-8">↵<title>Error</title>↵</head>↵<body>↵<pre>Cannot GET /api/patient/$%7BpatientUUID%7D/DossierEntry/all</pre>↵</body>↵</html>↵"
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure response for http://localhost:4200/api/patient/$%7BpatientUUID%7D/DossierEntry/all: 404 Not Found"
name: "HttpErrorResponse"
ok: false
status: 404
statusText: "Not Found"
url: "http://localhost:4200/api/patient/$%7BpatientUUID%7D/DossierEntry/all"
__proto__: HttpResponseBase
getDossierEntry( patientUUID: string, type: String = '' ): Observable<DossierEntry[]> {
patientUUID = '0d584905-fc20-4723-870a-0f0214419507';
const entryType = type === '' ? 'all' : 'type/' + type;
console.log(this.http.get<DossierEntry[]>('/api/patient/' + patientUUID + '/DossierEntry/' + entryType ));
console.log(patientUUID);
return this.http.get<DossierEntry[]>('/api/patient/' + patientUUID + '/DossierEntry/' + entryType );
}
return this.http.get<DossierEntry[]>('/api/patient/{patientUUID}/DossierEntry/' + entryType
{
"/api": {
"target": "http://localhost:1337",
"secure": false
}
}
ngOnInit() {
this.healthAPIService.getDossierEntry('physical').subscribe((result:DossierEntry[])=> {
console.log(result.values);
this.entries = result;
this.dossiersLoaded = true;
});
}
ngOnInit() {
this.healthAPIService.getDossierEntry('physical').subscribe((result:any)=> {
console.log(result.values);
this.entries = result;
this.dossiersLoaded = true;
});
}
getDossierEntry( patientUUID: string, type: String = '' ): Observable<DossierEntry[]> {
const entryType = type === '' ? 'all' : 'type/' + type;
return this.http.get<DossierEntry[]>('/api/patient/' + patientUUID + '/DossierEntry/' + entryType );
}
getDossierEntry( patientUUID: string, type: String = '' ): Observable<DossierEntry[]> {
const entryType = type === '' ? 'all' : 'type/' + type;
return this.http.get<DossierEntry[]>('/api/patient/${patientUUID}/DossierEntry/' + entryType);
}
console.log(this.http.get<DossierEntry[]>('/api/patient/' + patientUUID + '/DossierEntry/' + entryType)