0
我有一個PHP服務器頁面JSON格式返回數據是這樣的:使用的Ajax調用AngularJS
<?php
$data = array(
(object)array(
'title' => 'myfirstvalue',
'value' => 'myfirsttext',
),
(object)array(
'title' => 'mysecondvalue',
'value' => 'mysecondtext',
),
);
$json = json_encode($data);
echo $json;
?>
現在我需要顯示使用AngularJS的JSON title屬性。從教程採取我已經改編了以下代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
</head>
<body ng-app="myapp">
<div ng-controller="MyController" >
<button ng-click="myData.doClick(item, $event)">Send AJAX Request</button>
<br/>
Data from server: {{myData.fromServer}}
</div>
<script>
angular.module("myapp", [])
.controller("MyController", function($scope, $http) {
$scope.myData = {};
$scope.myData.doClick = function(item, event) {
var responsePromise = $http.get("http://localhost/myfunction.php");
responsePromise.success(function(data, status, headers, config) {
$scope.myData.fromServer = data.title;
});
responsePromise.error(function(data, status, headers, config) {
alert("AJAX failed!");
});
}
});
</script>
</body>
</html>
但是,當我點擊按鈕時沒有顯示。任何想法這個代碼的問題在哪裏? 謝謝!
如果你把'console.log(data)'成功回調,是否顯示數據已被接收? – vittore