angularJS --$http请求方式

来源:互联网 发布:淘宝特卖女装连衣裙 编辑:程序博客网 时间:2024/05/16 14:03
  1. 1,GET
  2. 2,POST
  3. 3,JSONP




  4. <!DOCTYPE html>  
  5. <html lang="zh_CN">  
  6. <head>  
  7.     <meta charset="UTF-8">  
  8.     <title>Angular基础</title>  
  9. </head>  
  10. <body>  
  11. <div ng-app="myApp">  
  12.     <div ng-controller="personCtrl">  
  13.         姓:<input type="text" ng-model="firstName"/><br/>  
  14.         名:<input type="text" ng-model="lastName"/><br/>  
  15.         姓名:<span ng-bind="firstName"></span><span ng-bind="lastName"></span>  
  16.     </div>  
  17.   
  18. </div>  
  19. <script src="angular.min.js"></script>  
  20. <script type="application/javascript">  
  21.     var myApp=angular.module('myApp',[]);  
  22.     myApp.controller('personCtrl',function($scope,$http){  
  23.         $http.get('getData.php').  
  24.                 success(function(data) {  
  25.                     console.log(data);  
  26.                 }).  
  27.                 error(function(err) {  
  28.                     //错误代码  
  29.                 });  
  30.         //$http.post采用postJSON方式发送数据到后台,  
  31.         // 解决办法:在后台php中使用$postData=file_get_contents("php://input",true);这样就可以获得前端传送过来的数据  
  32.         var postData={msg:'post的内容'};  
  33.         var config={params:{id:'5',name:'张三丰'}};  
  34.         $http.post('postData.php', postData,config).  
  35.                 success(function(data) {  
  36.                     console.log(data);  
  37.                 }).  
  38.                 error(function(err) {  
  39.                     //错误代码  
  40.                 });  
  41.         var myUrl ="http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1&callback=JSON_CALLBACK";  
  42.         $http.jsonp(myUrl).success(  
  43.                 function(data){  
  44.                     console.log(data);  
  45.                 }  
  46.         ).error(function(err){  
  47.                    //错误代码  
  48.                 });  
  49.         $scope.firstName="Wang";  
  50.         $scope.lastName="Ben";  
  51.     });  
  52.   
  53.   
  54. </script>  
  55. </body>  
  56. </html> 
0 0