2017-04-08 41 views
2

System.Exception: Call to Node module failed with error: Error: URLs requested via Http on the server must be absolute. URL: /Account/GetUser/System.Exception:調用節點模塊失敗,出現錯誤:錯誤:通過服務器上的HTTP請求的URL必須絕對

從Angular Universal調用http時發生此錯誤。從服務器,我無法訪問'位置'對象。

爲什麼在Angular阻止從相對URL調用Web服務時存在限制?

如何在不使用「位置」的情況下檢索主機名?

回答

0

我剛剛發現它被注入。

import { InjectionToken } from '@angular/core'; 

export const ORIGIN_URL = new InjectionToken<string>('ORIGIN_URL'); 
2

我通過進口下列組件固定該...

import { Component, Inject } from '@angular/core';

我再構造我的組件如

constructor(http: Http, @Inject('ORIGIN_URL')originUrl: string) {...

然後,當我想調用我的API我這樣做:

http.get(originUrl + 'YOUR API URI HERE').subscribe(result => {...});

相關問題