System.Exception:调用节点模块失败,错误:错误:服务器上通过Http请求的URL必须是绝对的

时间:2017-04-08 20:54:34

标签: angularjs node.js

  

System.Exception:调用节点模块失败,错误:错误:URL   通过服务器上的Http请求必须是绝对的。网址:   /帐户/的getUser /

从Angular Universal调用http时会发生此错误。从服务器,我无法访问“位置”对象。

为什么Angular会阻止从相对网址调用网络服务?

如何在不使用“位置”的情况下检索主机名?

2 个答案:

答案 0 :(得分:3)

我通过导入以下组件来解决这个问题......

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

然后构建我的组件,例如

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

然后当我想调用我的API时,我这样做:

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

答案 1 :(得分:0)

我刚发现它被注射了。

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

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