[英]$http.get JSON from PHP not working
首先,我是編程新手,我不是母語為英語的人,所以請原諒我在下面的問題中正確命名等方面的任何錯誤:)
這是我的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
顯示JSON內容時
<div ng-repeat="day in days">
<p> {{day.Year}} </p>
</div>
我在瀏覽器中看到了空白頁面。 沒有任何錯誤消息,沒有。
請幫助我,因為我一直在努力解決這個問題兩天,閱讀許多不同的解釋,評論等等。 我真的卡住了:(
好。 我根據評論的建議更改了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().'}';
}
});`
但問題仍然存在。
在回顯JSON時將Content-Type設置為application/json
。 這樣JavaScript會自動將響應字符串解析為JSON,否則你必須通過JSON.parse()
手動轉換響應來實現。
您的PHP代碼也容易出錯,如果拋出的異常不是PDOException
或者不繼承它,那么您將不會獲得有關該錯誤的信息。
您是否嘗試過從SLIM返回JSON響應 ? 除非您的數據庫查詢成功,否則這應該有效:
$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;
return $response->withJson($dbcalendar);
} catch(PDOException $e) {
$error = array('error' => array('text' => $e->getMessage()));
return $response->withJson($error,500);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.