📜  在 cpanel 中启用 CORS - 无论代码示例

📅  最后修改于: 2022-03-11 14:57:12.659000             🧑  作者: Mango

代码示例1
# I have tried somany codes but this work for me in "funtion.php" of active theme

add_action('init', 'handle_preflight');
function handle_preflight() {
    $origin = get_http_origin();
    if ($origin === 'http://yourdomain.com') {
        header("Access-Control-Allow-Origin: http://yourdomain.com");
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
        header("Access-Control-Allow-Credentials: true");
        header('Access-Control-Allow-Headers: Origin, X-Requested-With, X-WP-Nonce, Content-Type, Accept, Authorization');
        if ('OPTIONS' == $_SERVER['REQUEST_METHOD']) {
            status_header(200);
            exit();
        }
    }
}