私は以下のようにWebAPIでapiを作成しました。ランニングでHTTP API呼び出しの応答が未定義です - 角2
export class AppComponent implements OnInit {
data2: String;
constructor(private s: DemoService){}
ngOnInit(){
this.s.GetHttpData().subscribe(data=>this.data2=data);
console.log("Http call completed: "+this.data2);
}
:私は
Service.ts
@Injectable()
export class DemoService {
constructor(private http:Http){}
GetHttpData(){
return this.http.get('http://localhost:54037/api/home')
.map((res:Response)=>res.json());
}
コンポーネント以下のように角度からそれを呼び出すようにしようとしています
public HttpResponseMessage Get() {
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(JsonConvert.SerializeObject("Hello World"), Encoding.UTF8, "application/json");
return response;
}
アプリケーションは、私は出力を得る:完成
HTTP呼び出し:未定義
は、誰かがこれを助けることはできますか?
ありがとうございました
可能な重複https://stackoverflow.com/questions/43055706/how-do-i-return-the -response-from-observable-http-async-call-in-angular2) – Alex
[非同期呼び出しからの応答を返すにはどうすればよいですか?](https://stackoverflow.com/questions/14220321/how -do-i-return-from-as-asynchronous-call) – Igor
コードで予想される動作である重複を参照してください。 'console.log'文を' subscribe'コールバックの中に入れます。提案された複製を読んで、javascript/typescriptの非同期コードの基本を理解してください。 – Igor