Angular 8.3.20 http.get给出404

时间:2019-12-06 11:39:47

标签: angular angular-httpclient

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);
  }
...

屏幕截图: enter image description here 为什么?怎么样?什么?我做错了吗?

error

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

它可以与CORS相关。

CORS(跨域资源共享)是服务器说“ 即使您来自其他来源,我也会接受您的请求的一种方式。”

修改服务器以添加标头Access-Control-Allow-Origin:*,以从任何地方启用跨域请求(或指定域而不是*)。

我们对此api没有任何限制:

https://jsonplaceholder.typicode.com/todos

但是有了这个

http://ip.jsontest.com/

同时尝试两个地址...

Stackblitz Here