AngularJS implements $ http. post and $ http. get requests, angularjshttp. post
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 is not received, and the result is null, because you need to convert to 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 in 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. post request
The 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 that you build a real page in your mind and then use variables or placeholders to represent data, you can directly fill it in, while jquery dynamically modifies dom elements, such as adding and modifying dom labels. Different design ideas.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.