System.Exception:调用节点模块失败,出现错误:错误:通过服务器上的Http请求的URL必须是绝对的
System.Exception:调用节点模块失败,出现错误:错误:通过服务器上的Http请求的URL必须是绝对的。 URL:/ Account / GetUser /
从Angular Universal调用http时发生此错误。 从服务器,我无法访问“位置”对象。
为什么Angular阻止从相对URL调用Web服务?
如何在不使用“位置”的情况下检索主机名?
我通过导入以下组件来解决这个问题。
import { Component, Inject } from '@angular/core';
然后我构build我的组件,例如
constructor(http: Http, @Inject('ORIGIN_URL')originUrl: string) {...
然后,当我想调用我的API我这样做:
http.get(originUrl + 'YOUR API URI HERE').subscribe(result => {...});
我刚刚发现它被注入了。
import { InjectionToken } from '@angular/core'; export const ORIGIN_URL = new InjectionToken<string>('ORIGIN_URL');