How to use HTTP.GET in AngularJS correctly? In specific, for an external API call?

后端 未结 7 1411
轻奢々
轻奢々 2020-11-29 16:58

I have the following code in the controller.js,

var myApp = angular.module(\'myApp\',[]);

myApp.service(\'dataService\', function($http) {
delete $http.def         


        
相关标签:
7条回答
  • 2020-11-29 17:45

    When calling a promise defined in a service or in a factory make sure to use service as I could not get response from a promise defined in a factory. This is how I call a promise defined in a service.

    myApp.service('serverOperations', function($http) {
            this.get_data = function(user) {
              return $http.post('http://localhost/serverOperations.php?action=get_data', user);
            };
    })
    
    
    myApp.controller('loginCtrl', function($http, $q, serverOperations, user) {        
        serverOperations.get_data(user)
            .then( function(response) {
                console.log(response.data);
                }
            );
    })
    
    0 讨论(0)
提交回复
热议问题