-
Notifications
You must be signed in to change notification settings - Fork 2
ajax
Our project use AngularJS in the front-end, and AngularJS is a framework of Ajax.
-
[AngularJS ajax $http documentation] (https://docs.angularjs.org/api/ng/service/$http)
-
AngularJS has "$http" control, it send ajax call to the server in order to read data from server. And the server call the database to get the result that we want, this is how AngularJS use $http get json data from server.
for example: (from app.js)
// $http to get the cookie run.$inject = ['$rootScope', '$location', '$cookieStore', '$http']; function run($rootScope, $location, $cookieStore, $http) { // keep user logged in after page refresh $rootScope.globals = $cookieStore.get('globals') || {}; if ($rootScope.globals.currentUser) { $http.defaults.headers.common['Authorization'] = 'Basic ' + $rootScope.globals.currentUser.authdata; } $rootScope.$on('$locationChangeStart', function (event, next, current) { // redirect to login page if not logged in and trying to access a restricted page var restrictedPage = $.inArray($location.path(), ['/login', '/signup']) === -1; var loggedIn = $rootScope.globals.currentUser; if (restrictedPage && !loggedIn) { $location.path('/login'); } }
-
authentication.js use "$http" to get the authentication and check the author info is correct or not.
function authenticationService($http, $cookieStore, $rootScope, $timeout, userService) { var service = {};
service.login = login; service.setCredentials = setCredentials; service.clearCredentials = clearCredentials;
return service;
......
}
-
In userservice.js, each function below used ajax to get all json data.
userService.$inject = ['$http','$rootScope','$location','$cookies']; function userService($http,$rootScope,$location,$cookies) { var service = {};
service.getAllPost = getAllPost; service.newPost = newPost; service.getPost = getPost; service.deletePost = deletePost; service.editPost = editPost; service.getAllMyFriend=getAllMyFriend; service.getFriendPosts =getFriendPosts; service.createUser=createUser; service.getAuthorById=getAuthorById; service.newComment = newComment; service.deleteComment = deleteComment; service.updateAuthor = updateAuthor; service.getAllAuthor = getAllAuthor; service.removeFriend = removeFriend; service.sendFriendRequest = sendFriendRequest; service.getGithub = getGithub; service.requestSend=requestSend; service.acceptRequest = acceptRequest; service.rejectRequest = rejectRequest;
return service;
...
}