service.ts:
...
import { HttpClient, HttpHeaders } from '@angular/common/http';
...
constructor(private http: HttpClient) { console.log('shared service started'); }
...
getPosition() {
return this.http.get(`http://ip.jsontest.com/`);
}
...
position.component.ts:
...
ngOnInit() {
this.productService.getPosition().subscribe(data => this.profile = data);
}
...
答案 0 :(得分:1)
它可以与CORS相关。
CORS(跨域资源共享)是服务器说“ 即使您来自其他来源,我也会接受您的请求的一种方式。”
修改服务器以添加标头Access-Control-Allow-Origin:*,以从任何地方启用跨域请求(或指定域而不是*)。
我们对此api没有任何限制:
https://jsonplaceholder.typicode.com/todos
但是有了这个
http://ip.jsontest.com/
同时尝试两个地址...