php - browsers won't display JSON content automatically -
my php (slim framework) generating json file. have checked file on json validator , fine. when enter index.html, local, angularjs main file, on browser not display json file automatically asks me whether open/save file. i'm using apache virtual host, mysql, xampp. want browser display json without saving file locally, that, when finish project , attach domain, able display json content on computer.
this php file:
`<?php use \psr\http\message\serverrequestinterface request; use \psr\http\message\responseinterface response; $app = new \slim\app; // customers $app->get('/api/calendar', function (request $request, response $response) { // echo 'calendar'; }); $sql = "select * days"; try { // 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); } });` ok. have changed php file (i hope correctly) according fred suggestion:
`<?php use \psr\http\message\serverrequestinterface request; use \psr\http\message\responseinterface response; $app = new \slim\app; // customers $app->get('/api/calendar', function (request $request, response $response) { // echo 'calendar'; }); $sql = "select * days"; try { // 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); } });` but there still isn't change.
before output json string, set header, this:
header('content-type: application/json'); that should tell browser expect json content.
Comments
Post a Comment