为什么为get / post方法返回405错误Slim

时间:2019-12-10 15:50:34

标签: cors slim

全部

我对slim3中的CORS有一些疑问

我在tuupola / cors-middleware中使用苗条/苗条的骨架

index.php

    $app->add(new Tuupola\Middleware\CorsMiddleware([
        "origin" => "*",
        "methods" => ["GET", "POST"],
        "headers.allow" => [],
        "headers.expose" => [],
        "credentials" => false,
        "cache" => 0,
    ]));

routes.php

$app->post('/usertasks/{id}', function (Request $request, Response $response, array $args) use ($container) {
        // Sample log message
        $json = json_encode(array('id'=>$args['id']), JSON_PRETTY_PRINT);
        $response->getBody()->write($json);
        return $response->withHeader('Content-Type', 'application/json');
    });
    $app->get('/usertasks/{id}', function (Request $request, Response $response, array $args) use ($container) {
        // Sample log message
        $json = json_encode(array('id'=>$args['id']), JSON_PRETTY_PRINT);
        $response->getBody()->write($json);
        return $response->withHeader('Content-Type', 'application/json');
    });

.htaccess

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule ^$ public/     [L]
   RewriteRule (.*) public/$1 [L]
</IfModule>
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"

响应标题

enter image description here

我从另一台服务器调用ajax请求并收到405(不允许方法)错误(CORS)

$.ajax({
            type: "post",
            url: "https://server.com/usertasks/1542",
            crossDomain: true,
            success: function (response) {
                console.log(response);
            },
            error: function (xhr, status, error) {
                console.error(xhr, status, error);
            }
        });

控制台错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://server.com/usertasks/1542. (Reason: CORS header 'Access-Control-Allow-Origin' does not match 'https://site.ccc, *'

你能告诉我发生了什么吗?为什么代码会出现CORSD错误?

0 个答案:

没有答案