如何连接 angular10 post api 和 slim 4 php?
How to connect angular10 post api with slim 4 php?
我的 angular 应用程序在 http://localhost:4200/ 上运行,我的 Slim4 应用程序在 localhost:8080 上运行。当我尝试在 angular 和 slim 之间集成 APIS 时,GET API 工作正常,但 POST API 没有。我收到以下 CORS 错误,
从来源 'http://localhost:4200' 访问 'http://localhost:8080/admin/login' 处的 XMLHttpRequest 已被 CORS 策略阻止:Access-Control-Allow-Headers 中的 Access-Control-Allow-Headers 不允许请求 header 字段 cache-control预检响应。
我的 angular 请求 'content-type' 是 'applictaion/json'。请在下面找到 slim4 响应 header,
<?php
declare(strict_types=1);
namespace App\Application\ResponseEmitter;
use Psr\Http\Message\ResponseInterface;
use Slim\ResponseEmitter as SlimResponseEmitter;
class ResponseEmitter extends SlimResponseEmitter
{
/**
* {@inheritdoc}
*/
public function emit(ResponseInterface $response): void
{
// This variable should be set to the allowed host from which your API can be accessed with
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
$response = $response
->withHeader('Access-Control-Allow-Credentials', 'true')
->withHeader('Access-Control-Allow-Origin', $origin)
->withHeader(
'Access-Control-Allow-Headers',
'X-Requested-With, Content-Type, Accept, Origin, Authorization',
)
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
->withAddedHeader('Cache-Control', 'post-check=0, pre-check=0')
->withHeader('Pragma', 'no-cache');
if (ob_get_contents()) {
ob_clean();
}
parent::emit($response);
}
}
你试过吗?
->withHeader('Access-Control-Allow-Origin', '*')
我的 angular 应用程序在 http://localhost:4200/ 上运行,我的 Slim4 应用程序在 localhost:8080 上运行。当我尝试在 angular 和 slim 之间集成 APIS 时,GET API 工作正常,但 POST API 没有。我收到以下 CORS 错误,
从来源 'http://localhost:4200' 访问 'http://localhost:8080/admin/login' 处的 XMLHttpRequest 已被 CORS 策略阻止:Access-Control-Allow-Headers 中的 Access-Control-Allow-Headers 不允许请求 header 字段 cache-control预检响应。
我的 angular 请求 'content-type' 是 'applictaion/json'。请在下面找到 slim4 响应 header,
<?php
declare(strict_types=1);
namespace App\Application\ResponseEmitter;
use Psr\Http\Message\ResponseInterface;
use Slim\ResponseEmitter as SlimResponseEmitter;
class ResponseEmitter extends SlimResponseEmitter
{
/**
* {@inheritdoc}
*/
public function emit(ResponseInterface $response): void
{
// This variable should be set to the allowed host from which your API can be accessed with
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
$response = $response
->withHeader('Access-Control-Allow-Credentials', 'true')
->withHeader('Access-Control-Allow-Origin', $origin)
->withHeader(
'Access-Control-Allow-Headers',
'X-Requested-With, Content-Type, Accept, Origin, Authorization',
)
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
->withAddedHeader('Cache-Control', 'post-check=0, pre-check=0')
->withHeader('Pragma', 'no-cache');
if (ob_get_contents()) {
ob_clean();
}
parent::emit($response);
}
}
你试过吗?
->withHeader('Access-Control-Allow-Origin', '*')