Home > Web Front-end > JS Tutorial > body text

Detailed code explanation of angularJS implementing $http.post and $http.get requests

Y2J
Release: 2017-05-22 13:29:55
Original
1763 people have browsed it

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); 
})
Copy after login

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' 
  } 

});
Copy after login

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); 
})
Copy after login

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);});
    });
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!