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:
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.