AngularJS implements $ http. post and $ http. get requests, angularjshttp. post

Source: Internet
Author: User

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.

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.