【发布时间】:2017-08-09 12:17:11
【问题描述】:
我对 http.get 请求有疑问。 请求后数据显示1秒,然后消失。 组件.ts:
http.get('http://localhost:52875/api/clients/1').subscribe(result => {
this.client = result.json();
});
http.get('http://localhost:52875/api/client_balances/1').subscribe(result => {
this.cash = result.text();
});
component.html:
<p *ngIf="!client"><em>Loading...</em></p>
<form *ngIf="client">
{{client.first_name}}
{{client.last_name}}
<br />
{{cash}}
<br />
<a class="alert-link" [routerLink]="['/cash/add']">Add</a>
<a class="alert-link" [routerLink]="['/cash/out']">Out</a>
</form>
【问题讨论】:
-
浏览器是否出现异常?
-
代码本身似乎是正确的
-
broser中没有异常
-
我发现了错误:“XMLHttpRequest 无法加载 localhost:52875/api/products。请求的资源上没有 'Access-Control-Allow-Origin' 标头。因此不允许使用 Origin 'localhost:50749'访问。”
-
您的 Web 应用程序是否托管在
localhost:50749上?如果是这样,那么您正在处理 CORS 问题:en.wikipedia.org/wiki/Cross-origin_resource_sharing。要解决它,您基本上需要允许您的api接受来自其他来源的请求