我的PHP(SLIM框架)正在生成JSON文件。我检查了JSON验证器上的文件,一切都很好。当我在浏览器中输入index.html
(我的本地AngularJS主文件)时,它不会自动显示JSON文件,而会询问我是否打开/保存该文件。我使用Xampp的Apache虚拟主机MySQL。我希望浏览器在不保存本地文件的情况下显示JSON,这样,当我完成项目并将其附加到域时,任何人都可以在他的计算机上显示JSON内容。浏览器将不会自动显示JSON内容
这是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;
return $response->withJson($dbcalendar);
} catch(PDOException $e) {
$error = array('error' => array('text' => $e->getMessage()));
return $response->withJson($error,500);
}
});`
确定。根据Fred的建议,我已经更改了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;
return $response->withJson($dbcalendar);
header('Content-type: application/json');
} catch(PDOException $e) {
$error = array('error' => array('text' => $e->getMessage()));
return $response->withJson($error,500);
}
});`
但是仍然没有任何变化。
请提交一些代码 –
您是否向浏览器发送正确的接受和内容类型的标题为json文件? (提示:application/json) – RichardAtHome