3

System.Exception: Call to Node module failed with error: Error: URLs requested via Http on the server must be absolute. URL: /Account/GetUser/

This error happens when calling http from Angular Universal. From the server, I have no access to the 'location' object.

Why is there a restriction in Angular preventing calling web services from a relative url?

How can I retrieve the host name without using 'location'?

2 Answers 2

3

I fixed this by importing the following component...

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

I then construct my component e.g.

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

Then when I want to invoke my API I do this:

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

Sign up to request clarification or add additional context in comments.

Comments

0

I just discovered it is injected.

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.