中文字幕一区二区人妻电影,亚洲av无码一区二区乱子伦as ,亚洲精品无码永久在线观看,亚洲成aⅴ人片久青草影院按摩,亚洲黑人巨大videos

AngularJS Http

我們可以使用 AngularJS 內(nèi)置的 $http 服務(wù)直接同外部進(jìn)行通信。

$http 服務(wù)只是簡單的封裝了瀏覽器原生的 XMLHttpRequest 對(duì)象。


$http 是 AngularJS 中的一個(gè)核心服務(wù),用于讀取遠(yuǎn)程服務(wù)器的數(shù)據(jù)。

使用格式:

// 簡單的 GET 請(qǐng)求,可以改為 POST
$http({
????method: 'GET',
????url: '/someUrl'
}).then(function successCallback(response) {
????????// 請(qǐng)求成功執(zhí)行代碼
????}, function errorCallback(response) {
????????// 請(qǐng)求失敗執(zhí)行代碼
});

簡寫方法

POST 與 GET 簡寫方法格式:

$http.get('/someUrl', config).then(successCallback, errorCallback);
$http.post('/someUrl', data, config).then(successCallback, errorCallback);

此外還有以下簡寫方法:

  • $http.get
  • $http.head
  • $http.post
  • $http.put
  • $http.delete
  • $http.jsonp
  • $http.patch

更詳細(xì)內(nèi)容可參見:https://docs.angularjs.org/api/ng/service/$http


讀取 JSON 文件

以下是存儲(chǔ)在web服務(wù)器上的 JSON 文件:

/try/angularjs/data/sites.php

{ "sites": [ { "Name": "小白教程", "Url": "", "Country": "CN" }, { "Name": "Google", "Url": "www.google.com", "Country": "USA" }, { "Name": "Facebook", "Url": "www.facebook.com", "Country": "USA" }, { "Name": "微博", "Url": "www.weibo.com", "Country": "CN" } ] }

AngularJS $http

AngularJS $http 是一個(gè)用于讀取web服務(wù)器上數(shù)據(jù)的服務(wù)。

$http.get(url) 是用于讀取服務(wù)器數(shù)據(jù)的函數(shù)。

廢棄聲明 (v1.5)

v1.5 中$httpsuccesserror 方法已廢棄。使用 then 方法替代。

通用方法實(shí)例

AngularJS1.5 以上版本 - 實(shí)例

var app = angular.module('myApp', []); app.controller('siteCtrl', function($scope, $http) { $http({ method: 'GET', url: '/try/angularjs/data/sites.php' }).then(function successCallback(response) { $scope.names = response.data.sites; }, function errorCallback(response) { // 請(qǐng)求失敗執(zhí)行代碼 }); });

運(yùn)行代碼 ?

簡寫方法實(shí)例

AngularJS1.5 以上版本 - 實(shí)例

<div ng-app="myApp" ng-controller="siteCtrl"> <ul> <li ng-repeat="x in names"> {{ x.Name + ', ' + x.Country }} </li> </ul> </div> <script> var app = angular.module('myApp', []); app.controller('siteCtrl', function($scope, $http) { $http.get("/try/angularjs/data/sites.php") .then(function (response) {$scope.names = response.data.sites;}); }); </script>

運(yùn)行代碼 ?

AngularJS1.5 以下版本 - 實(shí)例

<div ng-app="myApp" ng-controller="siteCtrl"> <ul> <li ng-repeat="x in names"> {{ x.Name + ', ' + x.Country }} </li> </ul> </div> <script> var app = angular.module('myApp', []); app.controller('siteCtrl', function($scope, $http) { $http.get("/try/angularjs/data/sites.php") .success(function (response) {$scope.names = response.sites;}); }); </script>

運(yùn)行代碼 ?

應(yīng)用解析:

注意:以上代碼的 get 請(qǐng)求是本站的服務(wù)器,你不能直接拷貝到你本地運(yùn)行,會(huì)存在跨域問題,解決辦法就是將 Customers_JSON.php 的數(shù)據(jù)拷貝到你自己的服務(wù)器上,附:PHP Ajax 跨域問題最佳解決方案。

AngularJS 應(yīng)用通過 ng-app 定義。應(yīng)用在 <div> 中執(zhí)行。

ng-controller 指令設(shè)置了 controller 對(duì)象 名。

函數(shù) customersController 是一個(gè)標(biāo)準(zhǔn)的 JavaScript 對(duì)象構(gòu)造器。

控制器對(duì)象有一個(gè)屬性: $scope.names

$http.get() 從web服務(wù)器上讀取靜態(tài) JSON 數(shù)據(jù)。

服務(wù)器數(shù)據(jù)文件為:? /try/angularjs/data/sites.php。

當(dāng)從服務(wù)端載入 JSON 數(shù)據(jù)時(shí),$scope.names 變?yōu)橐粋€(gè)數(shù)組。

Note 以上代碼也可以用于讀取數(shù)據(jù)庫數(shù)據(jù)。