简体   繁体   English

Angular 2丢失了来自http.get请求的数据

[英]Angular 2 lost data from http.get request

I have a problem with http.get request. 我对http.get请求有问题。 The data is displayed for 1 second after the request, and then disappear. 数据在请求后显示1秒钟,然后消失。 component.ts: component.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: 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>

The problem was that I did not give access to the API. 问题是我没有访问该API。 I'm using ASP.NET Core. 我正在使用ASP.NET Core。 I just opened access to my WEB application in my Startup.cs: 我刚刚在Startup.cs中打开了对我的WEB应用程序的访问权限:

public void ConfigureServices(IServiceCollection services)
    {
        // Added this:
        services.AddCors(options =>
        {
            options.AddPolicy("AllowSpecificOrigin",
                builder => builder.WithOrigins("http://localhost:50749"));
        });
        //
        Other code
        //
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
    ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
        // Added this:
        app.UseCors("AllowSpecificOrigin");

        app.UseMvc();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM