0

I'm using below code to return string from the method if the http promise is resolved but control is not waiting until promise is returned and i'm seeing return object is method method in the switch.Could anyone please tell me what mistake i'm doing here!!

controller has below logic:

var status=function(action){
switch(action)
{
case 'create':
                return $scope.submitdata();
                break;
default: alert();
}

$scope.submitdata=function()
{
service.postdata($scope.formdata)
    .success(function(data){
     $scope.response=data;
       return 'SUCCESS';
     })
    .error(function(error){
          Console.log(error);
          return 'FAILURE';
     })

}

Service method:

this.postdata=function(data){
return $http.post(URL,DATA);
}
Vicky
  • 45
  • 2
  • 9
  • You can't return a value from a async callback function... you need to use a callback based processing – Arun P Johny Mar 15 '16 at 03:23
  • 1
    Possible duplicate of http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – Arun P Johny Mar 15 '16 at 03:23
  • If you use a promise API, then you can chain the result using a then function. Or you can try a callback pattern. – Jim Pedid Mar 15 '16 at 03:23
  • How is `Postdata` used? – Arun P Johny Mar 15 '16 at 03:24
  • You're best option would probably be to return the promise because it's async so you can't just return it's result but you can set the function to return the promise and resolve the promise in your later code. – Binvention Mar 15 '16 at 03:27
  • If you want to use promises, you'll need to `return` them. And you should banish `success` and `error`, use `then` and `catch` instead (which actually work like you expect). – Bergi Mar 15 '16 at 04:07
  • Your method names are totally convoluted. There's `submitdata`, `Postdata` and `postdata`, but I can't see which is called where. Can you please [edit] your question to fix these? – Bergi Mar 15 '16 at 04:08
  • @Bergi sorry for the confusion..I have edited the question..please let me know if that make sense!! – Vicky Mar 15 '16 at 04:33
  • @ArunPJohny I update the question..basically I'm setting returned data in the scope and I just want to return the status to inform the caller function that data posted successfully. If you could provide any sample example that would be great help. Thanks!! – Vicky Mar 15 '16 at 04:36
  • @Vicky see http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call for an example of how to handle this case – Arun P Johny Mar 15 '16 at 04:38

1 Answers1

1

The .success and .error methods ignore return values.

Also parameter values are case sensitive.

this.postdata=function(data){
    //THIS
    return $http.post(URL,data);
    //return $http.post(URL,DATA);
}

Deprecation Notice1

The $http legacy promise methods .success and .error have been deprecated. Use the standard .then method instead.


$scope.submitdataPromise = function() {
    return service.postdata($scope.formdata);
});

$scope.submitdataPromise
  .then(function onFulfilled(response){
    $scope.data = response.data;
    //return data for chaining
    return data;
}).catch(function onRejected(response){
    console.log(response.status);
    //throw to chain error
    throw response;
});

The submitdataPromise function immediately returns a promise. The promise is a container for future results of the XHR resolved either fulfilled or rejected. Those results can be retrieved by registering functions with the $q service to be invoked upon resolution.

The response object has these properties:

  • data – {string|Object} – The response body transformed with the transform functions.
  • status – {number} – HTTP status code of the response.
  • headers – {function([headerName])} – Header getter function.
  • config – {Object} – The configuration object that was used to generate the request.
  • statusText – {string} – HTTP status text of the response.

A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Note that if the response is a redirect, XMLHttpRequest will transparently follow it, meaning that the error callback will not be called for such responses.

-- AngularJS $http Service API Reference - General Usage

georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • Thank u for the reply.. I tried using then in the controller instead of success but saw the similar behavior so just wondering if you could provide me some sample code snippet!! – Vicky Mar 15 '16 at 04:40