2017-08-09 7 views
1

http.getリクエストに問題があります。 要求後1秒間データが表示されてから消えます。 component.ts:http.getリクエストから角2のデータが失われました

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> 
+0

ブラウザには何も例外はありませんか? – eddyP23

+0

コード自体が正しいようです – eddyP23

+0

ブロッサーに例外はありません –

答えて

0

問題は、私はAPIへのアクセス権を与えていないということでした。私はASP.NET Coreを使用しています。私は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(); 
    } 
関連する問題