How to communicate with the server:
1.$http: It is simple to encapsulate the native XMLHttpRequest object of the browser. $http can only accept a function of one parameter, which is an object that contains the configuration content used to generate the HTTP request. This function returns a promise object that has a success and error two methods.
$http ({method: ' GET ', url: '/api/users.json '}). Success (function (Data,status,headers,config) {///when the corresponding is ready to be called}). Error ( function (data,status,headers,config) {//Called when the response is returned in an error state});//The method actually returns a Promise object
2.$resource: Create a factory class for a resource that can be understood as a restful framework.
1. Create a Resource object var User = $resource ('/api/users/:userid.json ', {userId: ' @id '}});//2. Initiate a GET request in a restful format://Get/api/users/123user.get ({ID: ' 123 '}, function (RESP) {//Processing response succeeded}, function (ERR) {//Handling error});// Initiate a delete request in restful format://Delete/api/users/123user.delete ({}, {id: ' 123 '}, function (response) {//Handle successful Delete response}, function ( Response) {//Handle unsuccessful Delete response});//etc RESTful format request
ANGULARJS Learning: Communication