지난 며칠 동안이 문제로 고심하고 있습니다.
저는 PHP에서 슬림 프레임 워크 v3을 사용하여 API를 만들었습니다.
XMLHttpRequest의 http://192.168.0.5/project/api/testing를로드 할 수 없습니다 -이 : 내가 AngularJS와 $ HTTP에서 어떤 경로를 호출 할 때 같은
내가 콘솔 오류가 발생합니다. 'Access-Control-Allow-Origin'헤더가 요청 된 리소스에 없습니다. 따라서 원점 'http://localhost'은 액세스 할 수 없습니다.
<?php
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
require 'vendor/autoload.php';
require 'app/config/configuration.php';
$app = new Slim\App(["settings" => $config]);
spl_autoload_register(function ($class_name) {
require("app/" . $class_name . ".php");
});
$app->get("/testing",function(Request $request,Response $response){
generateResponse($response,array("message"=>"success"));
});
function generateResponse($response, $data)
{
return $response->withStatus(200)
->withHeader("Content-Type", "application/json")
->withHeader("Access-Control-Allow-Origin", "*")
->withHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
->withHeader("Access-Control-Allow-Headers", "Content-Type, Depth, User-Agent, X-File-Size,X-Requested-With, If-Modified-Since,X-File-Name, Cache-Control, x-hash-key, x-req-timestamp, user-language-pref, Content_Language")
->write(json_encode($data));
}
$app->run();
응답으로 헤더를 추가했습니다. 하지만 여전히 $ HTTP에서 그것은 오류를 던져 경로 테스트 API를 호출합니다.
나는 완전히 어리석은 무엇인가 어리석은 것을 알고있다. 이 문제를 해결하기 위해 나를 도와주세요.
감사
UPDATE : 지금 노력하고 PHP 파일에서 헤더를 추가함으로써 해결
. 헤더 ('액세스 제어 허용 - 원본 : *');
헤더 ('액세스 제어 허용 방법 : GET, PUT, POST, DELETE, OPTIONS'); 헤더 ('액세스 제어 허용 헤더 : Content-Type, Content-Range, Content-Disposition, Content-Description');
이 링크는 며칠 편리 할 수도 있습니다 유의하시기 바랍니다 : - Click to go->
문제는 CORS를 구현하지 않았다는 것입니다. http://stackoverflow.com/questions/38005829/cors-post-request-fails/38035128#38035128 – geggleto