angular http demo

js controller :

<script>
function Register_Contact( $scope , $http)
{

	$scope.age = 0;
	$scope.gender = 0;

	$scope.check = function( name)
	{

 		$http( { method : 'GET' , url : '<?php echo site_url('api/query/company_exist');?>' , params : { 'company_name' : name} } ).success(function( data ,status ){


 			$scope.data = data['response'];

 		});
	}

	$scope.setTarget = function( id)
	{
		$http( { method : 'GET' , url : '<?php echo site_url('api/query/company_information.json');?>' , params : { 'id' : id } } ).success(function( data , status ){

			if( status == '200')
			{
				
				$scope._id = data.id;
				$scope._name = data.name;
				$scope._email = data.email;
				$scope._industry = data.industry;
				$scope._phone = data.phone;
				$scope._fax = data.fax;
				$scope._address = data.address;
				$scope._turnover = data.turnover;
				$scope._employee = data.employee;
				$scope._weburl = data.weburl;
				$scope._more = data.more;

			}

		});
	}

	$scope.submit = function(){


		data = {
				'_name' : $scope._name , 
				'_email' : $scope._email , 
				'_phone' : $scope._phone , 
				'_fax'   : $scope._fax , 
				'_industry' : $scope._industry , 
				'_address' : $scope._address , 
				'_postid' : $scope._postid , 
				'_turnover' : $scope._turnover , 
				'_employee' : $scope._employee , 
				'_weburl' : $scope._weburl , 

				'name' : $scope.name , 
				'email' : $scope.email , 
				'age' : $scope.age , 
				'gender' : $scope.gender , 
				'job' : $scope.job , 
				'office_phone' : $scope.office_phone , 
				'office_fax' : $scope.office_fax , 
				'mobile' : $scope.mobile , 
				'private_phone' : $scope.private_phone , 
				'description' : $scope.description , 
			}

		$http( { method : 'POST' , url : '<?php echo site_url('api/query/register_contact.json');?>' , 
			data : $.param( data ) , 
		  	headers: {
   				'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  			}
		}).success( function(){

		});

	}
}
</script>

php :

class Query extends REST_Controller
{

	public function company_exist_get()
	{	

		$name = trim( str_replace('/[^a-zA-Z0-9\_\.\s]/', '', $_GET['company_name']) );

		$list = $this->db->select('id,name')->from('admin_company')->like( array('name'=>$name))->get()->result_array();

		if( empty( $list))
			$list[0] = array('id' => 0 , 'name' => '暂无同名公司,可以使用');

		$this->response( array( 'response'=>$list ), 200);
	}

	public function company_information_get()
	{
		$id = intval( $_GET['id']);

		if( $id === 0)
			$this->response( array('response' => 'id error') , 400 );

		$information = $this->db->select('*')->from('admin_company')->where( array('id'=>$id))->get()->row_array();

		$this->response(  $information , 200 );
	}
	public function register_contact_post()
	{
		var_dump( $_POST);
	}

}
$http 的 post 发送参数的时候 记住参数要用 $.param() 处理


你可能感兴趣的:(angular http demo)