“http://localhost:8100”已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头

问题描述 投票:0回答:1

`从源“http://localhost:8100”访问“http://localhost/phpfile/leave-option.php”处的 XMLHttpRequest 已被 CORS 策略阻止:没有“Access-Control-Allow-Origin”标头存在于所请求的资源中。

<?php
// Handle preflight OPTIONS request
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    // Allow only specific origins instead of '*'
    $allowed_origins = array('http://localhost:8100');
    $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
    if (in_array($origin, $allowed_origins)) {
        header("Access-Control-Allow-Origin: " . $origin);
    }
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
    header("Access-Control-Allow-Headers: Content-Type, Authorization");
    exit; // Stop further execution
}


$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pis";
$connect = new mysqli($servername, $username, $password, $dbname);

// Execute query to fetch options
$query = "SELECT id, TypeofLeave AS name FROM leave_info"; // fixed sql query
$result = mysqli_query($connect, $query);

// Fetch data from the result set
$options = [];
while ($row = mysqli_fetch_assoc($result)) {
    $options[] = $row;
}

// Return options as JSON
header('Content-Type: application/json');
echo json_encode($options);
?>

获取数据并将其显示在离子选择选项上`

php node.js angular ionic-framework phpmyadmin
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.