私はここ数日からこの問題に苦労しています。
私はPHPでslim framework v3を使用してapiを作成しました。
のXMLHttpRequest http://192.168.0.5/project/api/testingをロードすることはできません - :私はangularjs $ HTTPから任意の経路を呼び出すときのよう
iは、コンソールのエラーを取得します。要求されたリソースに「Access-Control-Allow-Origin」ヘッダーが存在しません。 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ファイルのヘッダを付加することにより、溶液
。 ヘッダー( 'Access-Control-Allow-Origin:*');
ヘッダー( 'アクセス制御許可メソッド: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