RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
AngularJS中$http模块POST请求的实现方法

小编给大家分享一下AngularJS中$http模块POST请求的实现方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

创新互联是一家专注网站建设、网络营销策划、重庆小程序开发、电子商务建设、网络推广、移动互联开发、研究、服务为一体的技术型公司。公司成立10年以来,已经为1000多家成都搬家公司各业的企业公司提供互联网服务。现在,服务的1000多家客户与我们一路同行,见证我们的成长;未来,我们一起分享成功的喜悦。

一、代码如下:

$http({ 


  method:'post', 

  url:'post.php', 

  data:{name:"aaa",id:1,age:20} 

}).success(function(req){ 

  console.log(req); 

})

解决方案:

1、 

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("&");

  2.  

$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);

})

 php

 $rawpostdata = file_get_contents("php://input");
 $post = json_decode($rawpostdata, true);
 //传的数据都在$post中了;

二、 $http请求数据主要会有以下三种方式

1.get请求

2.post请求

3.jsonp




  
  Angular基础



  
    姓:
    名:
    姓名:   
  var myApp=angular.module('myApp',[]);   myApp.controller('personCtrl',function($scope,$http){     $http.get('getData.php').         success(function(data) {           console.log(data);         }).         error(function(err) {           //错误代码         });     //$http.post采用postJSON方式发送数据到后台,     // 解决办法:在后台php中使用$postData=file_get_contents("php://input",true);这样就可以获得前端传送过来的数据     var postData={msg:'post的内容'};     var config={params:{id:'5',name:'张三丰'}};     $http.post('postData.php', postData,config).         success(function(data) {           console.log(data);         }).         error(function(err) {           //错误代码         });     var myUrl ="http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1&callback=JSON_CALLBACK";     $http.jsonp(myUrl).success(         function(data){           console.log(data);         }     ).error(function(err){           //错误代码         });     $scope.firstName="Wang";     $scope.lastName="Ben";   });

显示效果:

AngularJS中$http模块POST请求的实现方法

以上是“AngularJS中$http模块POST请求的实现方法”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


标题名称:AngularJS中$http模块POST请求的实现方法
文章源于:http://scyingshan.cn/article/pssjsi.html