duanke3985 2019-02-19 09:21
浏览 912
已采纳

如何修复PHP中的CORS错误? (CORS未成功)

So I'm trying to make an endpoint to access my database with and it works just fine in postman, but when calling the GET request on my website i get a CORS error:

Query for foreign site blocked: The same origin policy does not allow remote resource reading http://IPGOESHERE/cordova/endpoint/log.php?id=-1. (Cause: The CORS query failed).)

I've tried googling but was unable to find anything useful.

My server-sided code is in 2 files which i have included below:

models/Log.php:

class Log {

  // database connection and table name
  private $conn;
  private $table_name = "logging";

  // object properties
  public $ID;
  public $UserID;
  public $Handling;
  public $Date;

  // constructor with $db as database connection
  public function __construct($db) {
      $this->conn = $db;
  }

  // read products
  function read($id) 
  {
        $query = "SELECT * FROM " . $this->table_name;

        if ($id != '-1') {
            // select query
            $query .= " WHERE logging.ID = ".$id;
        }
        // prepare query statement
        $stmt = $this->conn->prepare($query);

        // execute query
        $stmt->execute();

        return $stmt;
    }
}

log.php

// required headers
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Max-Age: 1000");
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding");
header("Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS, DELETE");
header("Content-Type: application/json; charset=UTF-8");

// database connection will be here
include_once '../database.inc';
include_once '../models/Log.php';

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  $id = $_GET['id'];

  $database = new Database();
  $db = $database->getConnection();

  $Log = new Log($db);
  // query products
  $stmt = $Log->read($id);
  $num = $stmt->rowCount();

  // check if more than 0 record found
  if ($num > 0) {
      $products_arr = array();
      $products_arr["records"] = array();

      while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
          extract($row);

          $product_item = array(
              "ID" => $ID,
              "UserID" => $UserID,
              "Handling" => $Handling,
              "Date" => $Date,
          );

          array_push($products_arr["records"], $product_item);
      }

      // set response code - 200 OK
      http_response_code(200);

      // show products data in json format
      echo json_encode($products_arr);
  } else {
      // set response code - 404 Not found
      http_response_code(404);

      // tell the user no products found
      echo json_encode(
          array("message" => "No log found")
      );
  }
}
  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 关于学生成绩的一个系统(语言-c语言)
      • ¥60 matlab传染病模型分段
      • ¥15 关于#matlab#的问题,如何解决?
      • ¥15 51单片机汇编语言Keil5基础问题!
      • ¥15 CygwinPortable 中 NumPy 无法安装
      • ¥15 vivado如何支持多维打包数组模式
      • ¥15 请问第13题到底应该怎么做
      • ¥15 stable diffusion报错问题
      • ¥20 数据可视化综合运用 导入npz文件以及读取内容
      • ¥15 倒计时汇编语言RTC实时时钟