Angularjs使用 ajax实例
HTML文件:
<!DOCTYPE html> <html ng-app='a72'> <head> <meta charset="UTF-8"> <title>使用$http快捷方式与服务端交互</title> <script src="../angular.min146.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div class="frame" ng-controller='c72'> <div class="show"> <div class="tip">POST返回的结果是:{{result}}</div> <button ng-click="onclick()">发送</button> </div> </div> <script> angular.module('a72',[]).config(function($httpProvider){ $httpProvider.defaults.transformRequest=function(obj){ var arrstr=[]; for(var p in obj){ arrstr.push(encodeURIComponent(p)+"="+encodeURIComponent(obj[p])) } return arrstr.join("&"); } $httpProvider.defaults.headers.post={ 'Content-Type':'application/x-www-form-urlencoded' } }) .controller('c72',function($scope,$http){ var postdata={name:"陶国荣"} $scope.onclick=function(){ $http.post('post.php',postdata).success(function(data,status,headers,config){ $scope.result=data; }) } }) </script> </body> </html>
PHP文件:
<?php $str=$_POST['name']; if($str=='陶国荣'){ echo "验证正确!"; }else{ echo "验证失败!"; } ?>
寅春树:爱设计,爱前端,更爱程序
QQ:41676015