ANGULARJS Learning: Communication

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.