6.$http服務(wù)

1.把文件夾,創(chuàng)建在服務(wù)器的目錄當(dāng)中。

2.在瀏覽器當(dāng)中,查看文件
http://localhost/day5-code/

3.到phpstorm當(dāng)中,進(jìn)行配置 setting ->deployment

4.點(diǎn)擊綠色添加,把文件地址,貼入進(jìn)去。

Ajax的基本原理復(fù)習(xí)


image.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--
設(shè)置在瀏覽器中運(yùn)行可以直接跳到本機(jī)127.0.0.1 或者  location
setting->deployment-> + name:php 隨便寫->選擇Local or menthed
-->
<script>
   /* 向服務(wù)器要數(shù)據(jù)*/
   var xhr = new XMLHttpRequest();
   /*想哪個(gè)服務(wù)器要請求*/
   xhr.open("get",'zm.php');
   /*開始要數(shù)據(jù)*/
   xhr.send();
   /*監(jiān)聽服務(wù)器有沒有把數(shù)據(jù)給我*/
   xhr.onreadystatechange = function () {
       /*收到了服務(wù)器給的數(shù)據(jù)*/
      if(xhr.readyState == 4 && xhr.status ==200){
          /*responseText 就是服務(wù)器給的數(shù)據(jù)*/
          console.log(xhr.responseText);
      }
   };
</script>
</body>
</html>
image.png

$http服務(wù)

$http服務(wù)-get請求

沒有傳遞參數(shù)的情況

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script src=angular.js></script>
<script>
    /*1.創(chuàng)建模塊*/
    var app = angular.module('app',[]);
    /*2.創(chuàng)建控制器*/
    app.controller("zmController",["$scope","$http",function($scope,$http){
        //以前都是我自己傳入數(shù)據(jù).現(xiàn)在我從數(shù)據(jù)庫中傳入
//        $scope.res="zm";
        $http({
            url:'zm.php',/*請求地址*/
            method:'get',/*請求類型*/
        }).success(function (res) /*res是請求地址中的內(nèi)容*/ {/*請求成功時(shí),回調(diào)*/
            $scope.res=res;
        }).error(function (error){
            console.log(error);
        })
    }])
    /*3.綁定模板*/
    /*4.綁定控制器*/
</script>
<body ng-app='app' ng-controller='zmController'>
<p>{{res}}</p>
</body>
</html>
image.png

get的查詢方式的傳參方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script src=angular.js></script>
<script>
    /*1.創(chuàng)建模塊*/
    var app = angular.module('app',[]);
    /*2.創(chuàng)建控制器*/
    app.controller("zmController",["$scope","$http",function($scope,$http){
        $http({
            url: 'get.php', /*請求地址*/
            method: 'get', /*q請求類型*/
            params: {
                flag: 'zm' /*一定要與服務(wù)器那邊的值相同和相等*/
            }
        }).success(function (res) {/*請求成功時(shí),回調(diào)*/

            $scope.res = res; //拿到服務(wù)器的數(shù)據(jù)
        }).error(function(error){/*請求失敗時(shí),回調(diào)*/
            console.log(error);
        })

    }])

    /*3.綁定模板*/
    /*4.綁定控制器*/



</script>
<body ng-app='app' ng-controller='zmController'>
<p>{{res}}</p>
</body>
</html>
image.png
1.url: 'get.php?flag=zm',

 2.params: {
                flag: 'zm'     /*一定要與服務(wù)器那    邊的值相同和相等*/
            }

$http服務(wù)-post請求

post必須得要設(shè)置請求頭

headers: {'Content-Type':'application/x-www-form-urlencoded' }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script src=angular.js></script>
<script>
    /*1.創(chuàng)建模塊*/
    var app = angular.module('app',[]);
    /*2.創(chuàng)建控制器*/
    app.controller("zmController",["$scope","$http",function($scope,$http){
        $http({
            url: 'post.php', /*請求地址*/
            method: 'post', /*請求類型*/
           headers: {/*
           post必須得要設(shè)置請求頭
           默認(rèn),不加請求頭,是以json  Content-Type:application/json;
           charset=UTF-8

           以php為例,post是接收不到j(luò)son的,所以就會發(fā)生錯(cuò)誤 {}形式

           post接收的是formData 是key:value形式

           */
               'Content-Type':'application/x-www-form-urlencoded' },
//            data:{
///          *默認(rèn)是以json形式傳遞.*/
//                flag: 'zm' /*一定要與服務(wù)器那邊的值相同和相等*/
//            }

            data:'flag=zm'

        }).success(function (res) {/*請求成功時(shí),回調(diào)*/

            $scope.res = res;
        }).error(function(error){/*請求失敗時(shí),回調(diào)*/
            console.log(error);
        })

    }])

    /*3.綁定模板*/
    /*4.綁定控制器*/

</script>
<body ng-app='app' ng-controller='zmController'>
<p>{{res}}</p>
</body>
</html>
image.png

$http服務(wù)-get請求的一個(gè)小demo

系統(tǒng)給出的是一個(gè)json文件,要使用此文件首先需要轉(zhuǎn)化成php文件格式

image.png
image.png
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script src=angular.js></script>
<script>
    /*1.創(chuàng)建模塊*/
    var app = angular.module('app',[]);
    /*2.創(chuàng)建控制器*/
    app.controller("zmController",["$scope",'$http',function($scope,$http){
        $http({
            url:'students.php',
            method:'get',
            params:{
                flag:'zm'
            }
        }).success(function(res){
            $scope.students=res;
            //接受服務(wù)器的值
        }).error(function(error){
            console.log(error);
        })
    }])

    /*3.綁定模板*/
    /*4.綁定控制器*/

</script>
<body ng-app='app' ng-controller='zmController'>
<ul>
    <li ng-repeat='student in students'>  //遍歷服務(wù)器的值
    {{student.name}}:{{student.address}}:{{student.age}}
    </li>
</ul>
</body>
</html>
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,578評論 19 139
  • 一、概念(載錄于:http://www.cnblogs.com/EricaMIN1987_IT/p/3837436...
    yuantao123434閱讀 8,742評論 6 152
  • Http協(xié)議詳解 標(biāo)簽(空格分隔): Linux 聲明:本片文章非原創(chuàng),內(nèi)容來源于博客園作者M(jìn)IN飛翔的HTTP協(xié)...
    Sivin閱讀 5,345評論 3 82
  • HTTP概述 超文本傳輸協(xié)議(HTTP,HyperText Transfer Protocol) 是互聯(lián)網(wǎng)上應(yīng)用最...
    曹淵說創(chuàng)業(yè)閱讀 3,954評論 2 61
  • 深入淺出HTTP協(xié)議(WEB開發(fā)和面試必備) 1.基礎(chǔ)概念篇 a.簡介 HTTP是Hyper Text Trans...
    半世韶華憶闌珊閱讀 1,343評論 0 7

友情鏈接更多精彩內(nèi)容