如何在AngularJS控制器中调用API?

2024-04-20

我需要在 AngularJS 控制器中执行这些 API 调用。

任何例子都会有帮助。

app.post('/user/auth', users.auth);
app.get('/user/logout', helpers.isAuthenticated, users.logout);

您只需要利用$http service https://docs.angularjs.org/api/ng/service/%24http像这样:

angular.module('app', [])
   .controller('ctrlname', ['$http', function($http){

    //POST sample
    $http.post('/user/auth', users.auth).then(function(response){
         //handle your response here
    });

    //GET sample
    //substitute 'param1' and 'param2' for the proper name the API is expecting these parameters
    $http.get('/user/logout/?param1=' + helpers.isAuthenticated + '&param2=' + users.logout).then(function(response){
         //handle your response here
    });

   }]
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在AngularJS控制器中调用API? 的相关文章

随机推荐