首页 > 技术文章 > angularjs $http.get 和 $http.post 传递参数

swarb 2017-04-25 16:54 原文

$http.get请求数据的格式

[js] view plain copy
  1. $http.get(URL,{  
  2.     params: {  
  3.         "id":id  
  4.     }  
  5. })  
  6. .success(function(response, status, headers, config){  
  7.       
  8. })  

 

$http.post请求数据的格式

[js] view plain copy
  1. $http.post(URL,{  
  2.     "id":id  
  3. })  
  4. .success(function(response, status, headers, config){  
  5.   
  6. })  

 

tips:

get 和 post方法传递参数的方式不一样

 

[java] view plain copy
  1. //分享商品 list 列表  
  2. .factory("shareGoodsListService",["$http",function($http){  
  3.     return {  
  4.         //得到所有的收货地址  
  5.         updateAddress : function(__scope__,addrid){  
  6.   
  7.             //获取地址的值  
  8.             var Province = $("select[name='Province']").val();  
  9.             var City = $("select[name='City']").val();  
  10.             var Area = $("select[name='Area']").val();  
  11.   
  12.             var formData = {  
  13.                 id:addrid,  
  14.                 name:__scope__.formData.name,  
  15.                 mobile:__scope__.formData.mobile,  
  16.                 province:Province,  
  17.                 city:City,  
  18.                 area:Area,  
  19.                 address:__scope__.formData.address,  
  20.                 zip:__scope__.formData.zip,  
  21.                 submit:"submit"  
  22.             };  
  23.   
  24.             $http.post("{:U('AddressInfo/editAddress')}",formData).success(function(response, status, headers, config){  
  25.                 if(response.status == 1){  
  26.                     alert("修改成功!");  
  27.                 }  
  28.             })  
  29.         },  
  30.         getAddress:function(){  
  31.             //$http.get方法 传递参数使用的是 {params:jsonObj}  
  32.             $http.get("{:U('AddressInfo/editAddress')}",{  
  33.                 params: {  
  34.                     "id":id  
  35.                 }  
  36.             })  
  37.             .success(function(response, status, headers, config){  
  38.                 //获取地址的数据  
  39.                 if(response.status == 1){  
  40.                     __scope__.formData = {  
  41.                         name:response.data.name,  
  42.                         mobile:response.data.mobile,  
  43.                         address:response.data.address,  
  44.                         zip:response.data.zip  
  45.                     };  
  46.                     //默认收货地址的值  
  47.                     new PCAS("Province","City","Area",response.data.province,response.data.city,response.data.area);  
  48.                 }  
  49.             })  
  50.         }  
  51.   
  52.     }  
  53. }])  

推荐阅读