2017-01-02 4 views
-1

Ich versuche, einen Rückruf vom eckigen Service zu bekommen, aber die Callback-Funktion funktioniert nicht.http Post Rückruf funktioniert nicht angular factory

Hier ist mein Controller-Code: -

$scope.uu = "hello" // this i just make for test 
$scope.login = function(user){ 
    //console.log(user) 
    AuthenticationService.Login(user.username,user.password).success(function(data){ 
     console.log(data); 
     $scope.uu = data; 
    }) 
} 

Mein Service-Code ist: -

mainApp.service('AuthenticationService',function($http,$cookieStore,$rootScope){ 
    var service = {}; 
    service.Login = function (username, password, callback) { 

      $http({ 
     method: "POST", 
     //headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
     url: 'users/authenticate', 
     data: {email: username,password:password} 
    }) 
    .success(callback); 
     }; 
     return service; 
}); 

Weder ich Antwort in der Konsole erhalten. Der Umfang wird nicht geändert. Ich habe auf diese Frage hingewiesen, aber die Antwort funktioniert nicht für mich. $http POST response from service to controller

+0

Überprüfen Sie, welchen Fehler Sie erhalten, während API-Aufruf –

+0

Ich bekomme die richtige Antwort von API. Die API antwortet. –

+0

nur der Callback funktioniert nicht, auch wenn ich console.log ('test') schreibe, funktioniert das nicht auch im Controller –

Antwort

1

Wenn Sie Service Schreiben sind der Code sollte wie folgt aussehen:

mainApp.service('AuthenticationService',function($http,$cookieStore,$rootScope){ 
 
    
 
    this.Login = function (username, password, callback) { 
 

 
      $http({ 
 
     method: "POST", 
 
     //headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
 
     url: 'users/authenticate', 
 
     data: {email: username,password:password} 
 
    }) 
 
    .success(callback); 
 
     }; 
 
     
 
});

Immer Service erinnern ist, wie Prototypfunktion
Wir sollten nicht Gegenstand in cas-of-Service zurückkehren . Eckig Instanziieren Sie das Objekt und automatisch

+0

Danke @ricky :) –