新聞中心
本篇文章為大家展示了如何在angularJs中使用$http獲取后臺(tái)數(shù)據(jù),內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、微信小程序、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了武侯免費(fèi)建站歡迎大家使用!
1.html
網(wǎng)站名稱 網(wǎng)址 {{v.name}} {{v.url}}
1.php
'百度', 'url' => 'www.baidu.com' ], [ 'name' => '騰訊', 'url' => 'www.qq.com' ], ]; echo json_encode($data,JSON_UNESCAPED_UNICODE);
上面是最簡(jiǎn)單的$http獲取后臺(tái)數(shù)據(jù)實(shí)例,假如一個(gè)頁(yè)面中要異步加載一個(gè)后臺(tái)文件好幾次,那么是不是也要請(qǐng)求服務(wù)好幾次呢?顯然這樣會(huì)使頁(yè)面的加載出現(xiàn)遲鈍,那么,我們可以通過(guò)緩存操作來(lái)減少服務(wù)器壓力,使其盡快顯示頁(yè)面數(shù)據(jù),那么,具體怎么做呢?很簡(jiǎn)單,在$http 中添加cache:true, ,即可解決,再刷新頁(yè)面的時(shí)候,只會(huì)顯示一次重復(fù)請(qǐng)求的數(shù)據(jù)。
$http({ method:'get', url:'1.php', cache:true, //避免多次請(qǐng)求后臺(tái)數(shù)據(jù) }).then(function(response){ //成功時(shí)執(zhí)行 console.log(response); $scope.data = response.data; },function(response){ //失敗時(shí)執(zhí)行 console.log(response); });
當(dāng)然,像jquery的ajax請(qǐng)求那樣,angularjs也可以進(jìn)行簡(jiǎn)寫,
m.controller('ctrl', ['$scope', '$http', function ($scope, $http) { //post方式 //$http.post('1.php',{id:1})參數(shù)里可加屬性 $http.post('1.php').then(function(response){ //成功時(shí)執(zhí)行 console.log(response); $scope.data = response.data; }); }]);
m.controller('ctrl', ['$scope', '$http', function ($scope, $http) { //get方式 //$http.get('1.php',{cache:true}) 參數(shù)里可加屬性 $http.get('1.php').then(function(response){ //成功時(shí)執(zhí)行 console.log(response); $scope.data = response.data; }); }]);
最后,來(lái)說(shuō)下 $http服務(wù)之后臺(tái)接收POST數(shù)據(jù)的幾種方式:
上述內(nèi)容就是如何在angularJs中使用$http獲取后臺(tái)數(shù)據(jù),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前文章:如何在angularJs中使用$http獲取后臺(tái)數(shù)據(jù)
文章源于:http://ef60e0e.cn/article/jiigdo.html