php - $http.get JSON 从 PHP 不工作

标签 php mysql angularjs json apache

首先,我是编程新手,我的母语不是英语,所以请原谅我在以下问题中正确命名等方面的任何错误:)

这是我的 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 上学到的版本。因此我使用 .success.error在我的里面 $http.get服务。

我还在我的 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 response from SLIM ?除非您的数据库查询成功,否则这应该有效:

$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);
    }

});

关于php - $http.get JSON 从 PHP 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43205329/

相关文章:

php - 如何解码/膨胀分块的 gzip 字符串?

mysql - 使用多列连接 2 个表

javascript - 如何使 Angular 模态可调整大小

php - php文件中的html内容不会在centos服务器中显示

php - 完成测验后结束用户的回合

php - Paypal API - GetVerifiedStatus - 无效请求

php - 在 mysql_query 中使用两个查询登录 php

mysql - 如何同时选择 ORDER BY 列和 RAND()?

javascript - 使用自定义 angularjs 指令加载脚本

javascript - 使用ngTable显示数据失败但没有错误