1

Im trying to get a json object from a http call with the following line.

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

When I log it

    console.log(u);

I dont get the json in return

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

How do i just make it return as a json string? Im using it in a factory if that matters. Thanks.

brondy
  • 35
  • 1
  • 9

3 Answers3

1

$http.get doesn't return the value which have been sent by API. It only returns the object of HttpPromise. To get the value you need to call the then function on the u

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

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

For more see the Documentation

Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
0

Assigning the http request to a variable doesnot mak the service call. You need to make the call as

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;
});

You can find more details about the promises and web service call here.

Nitheesh
  • 19,238
  • 3
  • 22
  • 49
  • Cant i make IT available outside the method? – brondy Dec 01 '16 at 07:04
  • You can assign the response to a scope variable or a global variable for this. – Nitheesh Dec 01 '16 at 07:08
  • I'm new to JavaScript , is a global var the same as a static variable? And how do I assign it to such variable so I can access it anywhere in my code? – brondy Dec 01 '16 at 07:10
  • Global variables are a kind of static variable. The main difference is that the accessibility of the Global variables are code dependent rather than the time dependence. The Global variable are actually available throughout the program. Since you are using angular JS I will recommend you to use scope variable. The life of scope variable extends throughout the controller. – Nitheesh Dec 01 '16 at 07:15
  • You can find more about Local, Global and Static variable here.http://stackoverflow.com/questions/13415321/difference-between-static-auto-global-and-local-variable-in-the-context-of-c-a – Nitheesh Dec 01 '16 at 07:15
  • I dont get it. I have a return method which should return my u variable: all: function(){ return u; } Now how do I get that function to return the u variable in a correct manner. JS makes me want to gouge my eyes out lol – brondy Dec 01 '16 at 08:06
0

When you perform an HTTP request, this request is not completed instantly, but rather it's completed asynchronously. So what happens is that when the request is made you get a sort of token (a Promise) that you can keep track of while the request is 'in the air'.

This promise is the object you log when you type:

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

To 'keep track' of this promise you can supply it with functions, somewhat similar to event handlers by using the then, error, success, and finally functions.

So here's what happens:

// 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);
});

Note that I put 'event handler' in quotes because it helps to think of the functions as like-'event handlers', but there are some differences. Have a look at the documentation for the $q service for more information on what Promises are and how they work.

Robba
  • 7,684
  • 12
  • 48
  • 76