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 "验证失败!";
	}
?>

  

posted @ 2016-07-07 14:11  寅春树  阅读(335)  评论(0编辑  收藏  举报