2016-03-17 69 views
0

我在瀏覽器沒有發出HTTP呼叫的打字稿中有角2問題。瀏覽器中的網絡部分沒有看到任何HTTp調用。 角碼的作品,當我沒有任何HTTP服務的服務,它帶來的數據,當我使用HTTP它doenst作出任何http請求角2沒有從瀏覽器發出http呼叫

以下是我的代碼。

<script src="~/lib/es6-shim.min.js"></script> 
<script src="~/lib/system-polyfills.js"></script> 
<script src="~/lib/shims_for_IE.js"></script> 

<script src="~/lib/angular2-polyfills.js"></script> 
<script src="~/lib/system.js"></script> 
<script src="~/lib/Rx.js"></script> 
<script src="~/lib/angular2.dev.js"></script> 
<script src="~/lib/http.dev.js"></script> 
<script src="~/lib/router.dev.js"></script> 

服務:

import {Injectable} from 'angular2/core'; 
import {Http, Response} from 'angular2/http'; 
import {Observable}  from 'rxjs/Observable'; 

@Injectable() 
export class AuthService { 

    private _authPath = 'http://api/Auth'; 
    constructor(private http: Http) { } 
    getAuthSettings() { 

     return new Promise((resolve, reject) => { 

      return this.http.get(this._authPath).toPromise().then((val: Response) => resolve(val.text())); 

     }); 
    } 
    private handleError(error: Response) { 
     // in a real world app, we may send the error to some remote logging infrastructure 
     // instead of just logging it to the console 
     console.error(error); 
     return Observable.throw(error.json().error || 'Server error'); 
    } 

} 

主模塊

import { Component } from 'angular2/core'; 
import {AuthService} from './auth.service'; 
import {OnInit} from 'angular2/core'; 
import {HTTP_PROVIDERS} from 'angular2/http'; 

@Component({ 

    selector: "main-app", 
    template: "{{title}}", 
    providers: [HTTP_PROVIDERS, AuthService] 

}) 
export class AppComponent implements OnInit { 
    public title: string = ''; 
    constructor(private _authService: AuthService) { 
    } 
    ngOnInit() { 
     //this.title = "aaa"; 
     this._authService.getAuthSettings().then((val: string) => { 
      this.title = val; 

     }); 
     //this._authService.getAuthSettings().subscribe((val: string) => this.title = val) 
    } 

}; 

回答

1

我沒有看到在瀏覽器的網絡段中的任何HTTP調用。

可能有一些過濾器阻止您看到它。可以肯定的更新代碼如下:

console.log('starting'); 
this._authService.getAuthSettings() 
    .then((val: string) => { 
     this.title = val; 
     console.log('success'); 
    },() => console.log('fail')); 
console.log('waiting'); 

,看看哪些記錄發生。希望它有幫助。對不起,如果它不

+0

我有類似的問題。我在我的asp核心應用程序中設置了cors,我用postman調用了url,它給了我一個json。當我嘗試從服務中調用時,它什麼也不返回。我試着用你的代碼,它只顯示'等待'。 http://stackoverflow.com/questions/40030613/asp-net-core-api-cors-for-angular2-app – FacundoGFlores