Angularjs Getting Started tutorial with server (Ajax) Interaction Example "complete demo source download" _angularjs

Source: Internet
Author: User
Tags http request

This example describes the Angularjs interaction with server Ajax. Share to everyone for your reference, specific as follows:

ANGULARJS requests resources from the Web server are done through Ajax , and all operations are encapsulated in the $http service, $http service is a function that receives only one parameter, an object that completes some configuration of the HTTP request, function returns an object with the success and error two methods.

Usage is as follows:

$http ({method: ' post ', url: ' loginaction.do '
}). Success (function (data,status,headers,config) {
//Normal response callback
}). Error (function (data,status,headers,config) {
//Error response callback
});

The status code is 200-299, and the response is considered successful, the success method is invoked, the first parameter data is the server-side return, and the status is the response status code. The following two parameters are not used, this is not done here. Interested friends please refer to the ANGULARJS API documentation.

In addition, the $http service provides shortcut methods that simplify complex configurations and simply provide URLs. For example, for post requests, we can write the following:

$http. Post ("Loginaction.do")
. Success (function (data,status,headers,config) {
//Normal response callback
}). Error ( function (data,status,headers,config) {
//Error response callback
});

Let's look at a case:

 <! DOCTYPE html>  
 

Click the "Request" button, we use the $http service to request data to the server, the server response data format is usually a section of JSON, where we replace with a text file, Person.json content as follows:

{' name ': ' Rongbo_j ', ' age ': ' 23 '}

The returned data is placed in the parameter, and we can get the contents of the server response to display the data in the view.

Full Demo Instance code click here to download the site .

I hope this article will help you to Angularjs program design.

Related Article

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.