CORS: header存在,但是"if"语句对标头求反



我正在开发一个非常简单的应用程序。

概览如下:

我有一个web表单,其中URL包含一个变量。我正在抓取该变量的值并通过AJAX调用将其发送到我的web服务器(它托管在另一个域上)。一旦检索到,变量对SQL DB运行,如果找到匹配,我将该数据发送回我的web表单来做一些事情。

我已经定义了CORS头并设置为仅接受来自我的表单所在的原始源的数据。到目前为止,一切正常。

下面是正在工作的PHP代码:

<?php
// CORS headers to allow traffic from the form to run here
header('Access-Control-Allow-Origin: https:mydomain.com');
header('Content-Type: application/json');
//Retrieve the value passed from the Ajax script
$finderID = $_REQUEST['Finder'];    
//SQL Statement to connect, retrieve and parse out the data as JSON
$conn = new mysqli("localhost", "db_uname", "db_pswd", "db_table");
$result = $conn->query("SELECT * FROM onlineFinderLookup WHERE printID = $finderID");
$outp = array();
$outp = $result->fetch_all(MYSQLI_ASSOC);         

echo json_encode($outp);
?>

现在,我希望能够对该变量的值进行一些验证。我想知道它是否为数字并且大于0。如果是,那么连接到DB并获取我的数据。

我已经尝试过这样做了:

<?php
// CORS headers to allow traffic from the form to run here
header('Access-Control-Allow-Origin: https:mydomain.com');
header('Content-Type: application/json');
//Retrieve the value passed from the Ajax script
$finderID = $_REQUEST['Finder'];   
if ($finderID >= 0 && is_numeric($finderID) {
//SQL Statement to connect, retrieve and parse out the data as JSON
$conn = new mysqli("localhost", "db_uname", "db_pswd", "db_table");
$result = $conn->query("SELECT * FROM onlineFinderLookup WHERE printID = $finderID");
$outp = array();
$outp = $result->fetch_all(MYSQLI_ASSOC);         

echo json_encode($outp);
}
else {
}
?>

当我在其中运行带有IF语句的代码时,我的控制台向我抛出一个CORS错误,并说标题未定义。知道为什么吗?

另外,如果这似乎是一个安全的方式来收集数据从我的数据库,或者如果我在左边的领域与此。

谢谢!

更新:包括控制台错误

从源'webform'访问'webserver'的XMLHttpRequest已被CORS策略阻止:请求的资源上没有'Access- control - allow - origin '标头。

您在IF句末尾漏掉了一个)。正确的方法是:

if ($finderID >= 0 && is_numeric($finderID)) {

如果服务器抛出异常,导致报头不能正确发送,然后被浏览器阻止