2-16.Angular-$http服务-get请求

$http服务-get请求

1.接口文档 定接口

url:get.php
type:get
params:flag
参数必须是xmg
respond:如果flagshi xmg,返回数据
    {
        name:'aaa'
        add:bbb
    }
    如果不是非法访问


<?php

$res = $_GET['flag'];
if($res == 'xmg'){
    echo "获取到了服务器数据: sk666";
}else {
    echo "非法访问";
}


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body ng-app="app" ng-controller="skController">

<p>{{res}}</p>

</body>

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

        //1.不带参数请求
       /* $http({
            url:'xmg.php',//请求地址
            method:'get' //请求类型
        }).success(function (res) {//请求成功时, 回调
            $scope.res = res;
        }).error(function (error) {//请求失败时, 回调
            console.log(error);
        });*/

        //2.带参数请求
        /*$http({
            url:'getParam.php?flag=xmg',//请求地址
            method:'get' //请求类型
        }).success(function (res) {//请求成功时, 回调
            $scope.res = res;
        }).error(function (error) {//请求失败时, 回调
            console.log(error);
        });*/

        $http({
            url:'getparam.php',//请求地址
            method:'get', //请求类型
            params:{
                flag:'xmg'
            }
        }).success(function (res) {//请求成功时, 回调
            $scope.res = res;
        }).error(function (error) {//请求失败时, 回调
            console.log(error);
        });

    }]);

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

</script>

</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容