首先我是編程新手,並且我不是英語母語的人,所以請在下列問題中正確命名事件等,請原諒我的錯誤:)
這是我的JSON輸出:
[{"Year":"2017","Month":"1","Day":"1"},{"Year":"2017","Month":"1","Day":"2"},{"Year":"2017","Month":"1","Day":"3"},{"Year":"2017","Month":"1","Day":"4"},{"Year":"2017","Month":"1","Day":"5"},{"Year":"2017","Month":"1","Day":"6"},{"Year":"2017","Month":"1","Day":"7"},{"Year":"2017","Month":"1","Day":"8"},{"Year":"2017","Month":"1","Day":"9"},{"Year":"2017","Month":"1","Day":"10"},{"Year":"2017","Month":"1","Day":"11"},{"Year":"2017","Month":"1","Day":"12"},{"Year":"2017","Month":"1","Day":"13"},{"Year":"2017","Month":"1","Day":"14"},{"Year":"2017","Month":"1","Day":"15"},{"Year":"2017","Month":"1","Day":"16"},{"Year":"2017","Month":"1","Day":"17"},{"Year":"2017","Month":"1","Day":"18"},{"Year":"2017","Month":"1","Day":"19"},{"Year":"2017","Month":"1","Day":"20"},{"Year":"2017","Month":"1","Day":"21"},{"Year":"2017","Month":"1","Day":"22"},{"Year":"2017","Month":"1","Day":"23"},{"Year":"2017","Month":"1","Day":"24"},{"Year":"2017","Month":"1","Day":"25"},{"Year":"2017","Month":"1","Day":"26"},{"Year":"2017","Month":"1","Day":"27"},{"Year":"2017","Month":"1","Day":"28"},{"Year":"2017","Month":"1","Day":"29"},{"Year":"2017","Month":"1","Day":"30"},{"Year":"2017","Month":"1","Day":"31"}]
我已經檢查它valiator和被接受。
這是一個RESTful API,使用SlimApp,Apache虛擬主機,mysql數據庫。
這是一個
calendar.php
,從SQL表中獲取JSON內容:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
// Get All Customers
$app->get('/api/calendar', function (Request $request, Response $response) {
// echo 'CALENDAR'; });
$sql = "SELECT * FROM days";
try {
// Get DB Object
$dbcalendar = new dbcalendar();
//Connect
$dbcalendar = $dbcalendar->connect();
$stmt = $dbcalendar->query($sql);
$dbcalendar = $stmt->fetchAll(PDO::FETCH_OBJ);
// $dbcalendar = null;
echo json_encode($dbcalendar);
} catch(PDOException $e) {
echo '{"error": {"text": '.$e->getMessage().'}';
}
});`.
這裏從AngularJS是文件:
days.js
app.factory('days', ['$http', function($http) {
return $http.get('http://slimapp/api/calendar/index.html')
.success(function(data) {
return data;
})
.error(function(err) {
return err;
});
}]);`,
個DaysController.js
app.controller('DaysController', ['$scope', 'days', function($scope, days) {
days.success(function(data) {
$scope.days = data;
});
}]);,
app.js
var app = angular.module('CalendarApp', ['ngRoute']);
app.config(function ($routeProvider) {
$routeProvider
.when("/", {
controller: "DaysController",
templateUrl: "views/test.html"
})
.otherwise({
redirecTo: "/"
});
});
的test.html
<div ng-repeat="day in days">
<p> {{day.Year}} </p>
</div>.
當我在http://slimapp/api/calendar在瀏覽器輸入時,將所顯示的內容JSON如上所示。
當把其他代碼放入test.html
時,例如:<p> Hello world </p>
,瀏覽器中的所有內容都顯示正常。
我使用老版本的AngularJS1.X,因爲這是我在CodeCademy上學到的版本。因此我在$http.get
服務中使用.success
和.error
。
我在我的Chrome瀏覽器中還安裝了「Allow-Control-Allow-Origin *」擴展名。
當我想用test.html
<div ng-repeat="day in days">
<p> {{day.Year}} </p>
</div>
顯示JSON內容我得到的空白頁面在瀏覽器中。沒有任何錯誤信息,什麼都沒有。
請幫助我,因爲我一直在爲這個問題掙扎兩天,閱讀大量不同的解釋,評論等等。我真的卡住了:(
OK我已經根據從意見建議改變calendar.php
:
`<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
// Get All Customers
$app->get('/api/calendar', function (Request $request, Response $response) {
// echo 'CALENDAR'; });
$sql = "SELECT * FROM days";
try {
// Get DB Object
$dbcalendar = new dbcalendar();
//Connect
$dbcalendar = $dbcalendar->connect();
$stmt = $dbcalendar->query($sql);
$dbcalendar = $stmt->fetchAll(PDO::FETCH_OBJ);
// $dbcalendar = null;
echo json_encode($dbcalendar);
header("Content-type:application/json");
} catch(PDOException $e) {
echo '{"error": {"text": '.$e->getMessage().'}';
}
});`
,但問題依然存在。
將「Content-Type」標題設置爲「application/json」。Chrome瀏覽器上仍然有空白頁面。在Internet Explorer 11上,瀏覽器詢問是否打開/保存'calendar.json'文件。當我嘗試打開它時,結果與Chrome上的相似。 _但是我把文件保存在我的硬盤上,並改變了'$ http.get' service_ **裏面的路徑,並且它工作。**請告知還有什麼應該改變,所以我可以通過輸入本地主機根目錄在瀏覽器中,**正如我在問題中所述。只剩下一步:) – Rico11112016
好吧,它現在可以工作。感謝幫助! – Rico11112016