2017-04-06 3 views
-1

내 PHP (SLIM 프레임 워크)가 JSON 파일을 생성 중입니다. JSON 검사기에서 파일을 검사했는데 모든 것이 정상입니다. index.html, 내 로컬 AngularJS 주 파일을 브라우저에 입력하면 자동으로 JSON 파일이 표시되지 않지만 파일을 열거 나 저장할지 묻습니다. 저는 Xampp의 Apache 가상 호스트 인 MySQL을 사용하고 있습니다. 브라우저에서 파일을 로컬에 저장하지 않고 JSON을 표시하면 프로젝트를 완료하고 도메인에 연결하면 아무나 자신의 컴퓨터에 JSON 콘텐츠를 표시 할 수 있습니다.브라우저에서 자동으로 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; 

    return $response->withJson($dbcalendar); 

    } catch(PDOException $e) { 
     $error = array('error' => array('text' => $e->getMessage())); 
     return $response->withJson($error,500); 
    } 
});` 

OK :

는 PHP 파일입니다. 나는 프레드 제안에 따라 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); 
    } 
});` 

그러나 여전히 변화가 없다.

+0

일부 코드를 제출하십시오 –

+1

브라우저에 json 파일의 올바른 승인 및 콘텐츠 유형 헤더를 보내시겠습니까? (힌트 : application/json) – RichardAtHome

답변

0

는 출력 할 JSON 문자열 전에이 같은 헤더를 설정합니다.

+0

이제 작동합니다. 내 PHP 파일의 맨 처음에 다음 두 줄의 코드를 추가했습니다 :' Rico11112016

0

@RichardAtHome이 http 헤더에 콘텐츠 유형을 확인하고 괜찮 으면. JSON.stringify()를 사용하여 일반 문자열로 출력하거나 일부 형식을 지정할 수 있습니다. 이것이 사실이 아니라면 일부 코드 및 출력 스크린 샷과 함께 문제를 설명하십시오.

header('Content-type: application/json'); 

JSON 컨텐츠를 기대하는 브라우저를 말해야한다 :

https://www.w3schools.com/js/js_json_stringify.asp