I want to get external json file from a webserver

#1

Hi,

I want to get external json file from a webserver ( http://www.debsociety.nl/wp-content/data.json)
The code I made now is :

@Injectable({
providedIn: ‘root’
})
export class SymposiumData {
data: any;

constructor(public http: HttpClient, public user: UserData) {}

load(): any {
if (this.data) {
return of(this.data);
} else {
return this.http
.get(’/assets/symposium/data.json’)
.pipe(map(this.processData, this));
}
}

But everything I tried I still get the data from /assets/symposium/data.json (local).

Can somebody help me: what did I do wrong or what will be the right code.

Thanks in advance.

Cheers, erik

#2
  • abused any - it has no business being anywhere in any of this code
  • only asked HttpClient to get a local resource; you must include the entire remote URL
  • created a baffling second stale code path with the half-assed cache
  • passed an object method as a callback, which usually leads to mysterious execution context errors
#3

hi apropos,

Sorry I explained my problem really bad:
I tried to change the code towards:

@Injectable({
providedIn: ‘root’
})
export class SymposiumData {
data: any;

constructor(public http: HttpClient, public user: UserData) {}

load(): any {
this.http.get(’http://www.debsociety.nl/wp-content/data.json’)
.pipe(map(this.processData, this));
}
}

But then I get an error.

What did I do wrong.
Thx in advance, Erik

#4

It would help if you posted all the code necessary to reproduce the problem (for example, processData is missing, as is whatever is calling load()). Also, the exact error message is useful.