2016-12-01 5 views
1

次の行でhttp呼び出しからjsonオブジェクトを取得しようとしています。

var u = $http.get("http://localhost:8580/jettyjspconfiguration-example/sql?loc=locc"); 

私は

console.log(u); 

それをログに記録するとき、私は私はちょうどそれがJSON文字列として返すようにするにはどうすればよいリターン

Object { $$state: Object, success: $http/promise.success(), error: $http/promise.error() 

でJSONを取得いけませんか?それが重要であれば工場で使っています。ありがとう。

答えて

1

$http.get APIから送信された値は返されません。 HttpPromiseのオブジェクトのみを返します。値を取得するには、より多くの変数へのHTTPリクエストがサービスコールをMAK doesnot割り当てDocumentation

0

参照してくださいu

var u = $http.get("http://localhost:8580/jettyjspconfiguration-example/sql?loc=locc"); 

u.then(function(response){ 
     var yourVar = response.data; 
     console.log(yourVar); 
    }); 

then関数を呼び出す必要があります。あなたは、あなたが約束して、Webサービスの呼び出しhereの詳細を見つけることができます

var u = $http.get("http://localhost:8580/jettyjspconfiguration-example/sql?loc=locc"); 
$scope.ResponeData = null; 
u.then(function(response){ 
    // Your response will be available here only 
    $scope.ResponeData = response; 
}); 

として呼び出しを行う必要があります。

+0

私は方法の外でITを利用できますか? – brondy

+0

これに対してスコープ変数またはグローバル変数に応答を割り当てることができます。 – Nitheesh

+0

私はJavaScriptを初めて使っていますが、グローバル変数は静的変数と同じですか?そして、どのように私はそのような変数に割り当てることができますので、私のコードのどこにでもアクセスできますか? – brondy

0

HTTPリクエストを実行すると、このリクエストは即座に完了するのではなく、非同期で完了します。だから何が起きるのかは、リクエストが行われたときに、リクエストが「空中」である間に追跡できる一種のトークン(Promise)を取得することです。

この約束は、あなたが入力したときにログオブジェクトです、thenを使用して、イベントハンドラと少し似て、あなたは機能とそれを供給することができます。この約束の「追跡するため」に

var u = $http.get("http://localhost:8580/jettyjspconfiguration-example/sql?loc=locc"); 
console.log(u); 

errorsuccess 、およびfinally機能。

だからここに何が起こるかです:それは同じような'eventハンドラなどの機能を考えるのに役立ちますが、いくつかの違いがあるので、私は引用符で「イベントハンドラ」を入れ

// Start the request and get a promise that an answer will eventually come. 
var u = $http.get("http://localhost:8580/jettyjspconfiguration-example/sql?loc=locc"); 

// The request is handled asynchronously, so all we have now is the promise that 
// at some time there will be a result. 
console.log(u); 

// Assign 'event handlers' to the promise 
u.then(function(result) { 
    // This 'event handler' function is called when the async process completes 
    // successfully. You can now use the data as you please 
    doFancyStuffWithResultData(result.data); 
}, function(error) { 
    // This 'event handler' function is called when the async process has an error 
    console.error('Ohnoz, things went wrong', error); 
}); 

注意。約束とその仕組みの詳細については、$q serviceのドキュメントをご覧ください。

関連する問題