3-2 Angular-http请求Demo

http请求Demo

1.添加angular.js , student.json
2.stu.php 接口
3.给url传参

http://localhost:63342/day3-code/stu.php?flag=xmg

<?php
//设置页面内容是html, 编码格式是utf-8
//file_get_contents() 把整个文件读入一个字符串中
header( 'Content-Type:text/html;charset=utf-8');

$res = $_GET['flag'];
if($res == "xmg"){
    echo file_get_contents('student.json');
}else {
    echo "非法访问";
}

<body ng-app="app" ng-controller="skController">
<ur>
    <li ng-repeat="stu in stus">{{stu.name}}--{{stu.address}}--{{stu.age}}</li>
</ur>
</body>

<script src="angular.js"></script>
<script>
    //1.创建模块
    var app = angular.module('app', []);
    //2.创建控制器
    app.controller('skController', ['$scope', '$http',function ($scope, $http) {

        $http({
            url:'stu.php',
            method:'get',
            params:{
                flag:'xmg'
            }
        }).success(function (res) {
            console.log(res);
            $scope.stus = res
        }).error(function (error) {
            console.log(error);
        })
    }]);

    //3.绑定模块 ng-app='app'
    //4.绑定控制器
</script>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容