var final;
final = $http.get('http://localhost:9000/therapist_data',config)
.success(function(response) {
console.log("I got the data I requested");
var resdata = response;
console.log(resdata);
return resdata;
});
console.log(final);
応答データを返してfinal変数に格納しようとしていますが、promiseオブジェクトを取得しています。
実際のデータを返すにはどうすればよいですか?
私はあなたのコードに基づいてシリルの答えを開発しようとします:
var final;
final = $http.get('http://localhost:9000/therapist_data',config)
.success(function(response) {
console.log("I got the data I requested");
var resdata = response;
console.log(resdata);
return resdata;
});
console.log(final);
実行の順序は次のとおりです。
var final
$http.get('http://localhost:9000/therapist_data',config) .success();
:これにより、リクエストがトリガーされ、サーバーがリクエストに応答したときに、関数がコールバックとして正常に登録されます。console.log(final);
->まだ未定義です。応答を待ちません。これは、コールバックと非同期処理の基本であり、いつ実行されるかはわかりません。少なくとも、他のすべてのコードの後に実行されることがよくあります。angleJSでは、同期リクエストを実行する方法はありません。成功関数でコードを移動する必要があります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加