2

I'm trying to return a JSON object from a call to a slim PHP function. It returns the object like this:

if (count > 0) {
    echo json_encode(array("status" => "greater"));
} else {
    echo json_encode(array("status" => "lesser"));
}

In my javascript I call this slim function using a service:

$scope.statusResults = service.isGreater();

console.log($scope.statusResults);

but what I get back in the console is: e {$$state: Object}

If I drill down into e, I find $$state : Object

which I can drill down into to get: status:1 value:Object and one last time to finally get to status:"greater"

I have no idea what $$state is or why it returns this way. I wanted it to return as a simple JSON object and just be {status: "greater"}. Is there a way to get it to return like that? If not, how do I access status?

Thanks!

charlietfl
  • 170,828
  • 13
  • 121
  • 150
Nickknack
  • 827
  • 4
  • 12
  • 26
  • 1
    Looks like `isGreater` returns something like a promise? You cannot have a function *synchronously* return the result of Ajax call. See [How do I return the response from an asynchronous call?](http://stackoverflow.com/q/14220321/218196) for more general information (there might be Angular specific solutions). – Felix Kling Dec 23 '16 at 02:41
  • 1
    sounds like you're making an Ajax request at some point in your code. Can you show us that code as well? – CodeGodie Dec 23 '16 at 02:41
  • Where's the code for the service ? – charlietfl Dec 23 '16 at 02:51
  • 1
    @Felix Kling You were right, it was returning a promise. I just needed to include a .then to the service call and use the response in the callback function to test for count. Could you put your comment as an answer so I can give you credit for your help?:) – Nickknack Dec 23 '16 at 03:10
  • Since you are making a call to PHP to return Json Object, the call is an Ajax call in which case it is not synchronous and needs to be handled asynchronously using callbacks or promises. Also have a look at XMLHTTPRequest API provided by JS to make calls to the server. – Manish M Demblani Dec 24 '16 at 04:33
  • and can you elaborate irate more on what `service.isGreater` does – Manish M Demblani Dec 24 '16 at 04:34

0 Answers0