Home > Article > Web Front-end > Detailed code explanation of angularJS implementing $http.post and $http.get requests
This article mainly introduces the implementation method of angularJS initiating $http.post and $http.get requests. It also introduces the methods of $http.post and $http.get requests respectively. Those who are interested can learn more
AngularJS initiates $http.post request
The code is as follows:
$http({ method:'post', url:'post.php', data:{name:"aaa",id:1,age:20} }).success(function(req){ console.log(req); })
At this time you will find that the returned data cannot be received, and the result is null. This is Because it needs to be converted into form data.
Solution:
Configure $httpProvider:
var myApp = angular.module('app',[]); myApp.config(function($httpProvider){ $httpProvider.defaults.transformRequest = function(obj){ var str = []; for(var p in obj){ str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); } return str.join("&"); } $httpProvider.defaults.headers.post = { 'Content-Type': 'application/x-www-form-urlencoded' } });
Or configure it in the post:
$http({ method:'post', url:'post.php', data:{name:"aaa",id:1,age:20}, headers:{'Content-Type': 'application/x-www-form-urlencoded'}, transformRequest: function(obj) { var str = []; for(var p in obj){ str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); } return str.join("&"); } }).success(function(req){ console.log(req); })
AngularJS initiates $http. The post request
code is as follows:
app.controller('sprintCtrl', function($scope, $http) { $http.get("http://localhost:8080/aosapp/pt/service?formid=pt_aosapp_service_sprintlist&teamid=1") .success(function (response) {console.log($scope.sprintlist=response);}); });
In fact, what is the biggest difference between angularjs and jquery js? Angularjs is what you have built in your mind in advance page, and then use variables or placeholders to represent data. When the data comes, just fill it in directly; while jquery dynamically modifies dom elements, such as adding and modifying dom tags, etc. The design ideas are different.
【Related Recommendations】
1. Javacript Free Video Tutorial
2. js development code using Baidu Maps Organize
3. Use node.js to analyze the url output file and send it to the client
4. Detailed explanation of examples of JavaScript converting Chinese characters to Pinyin
5. Share 15 commonly used js regular expressions
The above is the detailed content of Detailed code explanation of angularJS implementing $http.post and $http.get requests. For more information, please follow other related articles on the PHP Chinese website!