405方法不允许-PUT(PHP)

时间:2019-11-04 22:00:22

标签: php api .htaccess

我在向我的API发送PUT请求时遇到问题。我得到了回应: “ 405方法不允许。此URL不允许使用请求的方法PUT。”

在我的htaccess中,我有:

<Limit GET POST OPTIONS>
  Order allow,deny
Allow from all
</Limit>

在php文件中,我有:

<?php
header('Access-Control-Allow-Origin: *');

    if (isset ($_GET['action']))
    {
        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS')
        {
            header('Content-Type: application/json');
            header('Access-Control-Allow-Methods: POST, DELETE, PUT');
            header('Access-Control-Max-Age: 3600');
            header('Access-Control-Allow-Headers: origin,x-csrftoken,content-Type');
            header('Access-Control-Allow-Credentials: true');
            die();
        }else
....
....
....
...
   } else if ($_SERVER['REQUEST_METHOD'] == 'PUT')
                if($action == 'update')
                {
                    $authResp = Auth::autorize($db, isset($_COOKIE['tkn']) ? $_COOKIE['tkn'] : null);
                    if($authResp->getCode() == 200){
                        $roomResp = menu::update($db, $data);
                        $authResp->addToMessage($roomResp->getMessage());
                    }
                    echo $authResp->genResponse();
                    die();                    
                }

要允许PUT / DELETE方法,我还需要设置多少?

1 个答案:

答案 0 :(得分:0)

问题不在于您的PHP,而在于您的.htaccess;您有<Limit GET POST OPTIONS>限制仅请求 允许GETPOSTOPTIONS。如果您还希望允许PUTDELETE,则需要将它们添加到允许的方法列表中:

<Limit GET POST OPTIONS PUT DELETE>