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

新闻中心

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

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

目前成都创新互联公司已为成百上千的企业提供了网站建设、域名、虚拟主机网站托管、企业网站设计、西乡塘网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

一、代码如下:

$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请求的实现方法”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联网站建设公司行业资讯频道!

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站栏目:AngularJS中$http模块POST请求的实现方法-创新互联
URL链接:http://cqwzjz.cn/article/dddshd.html