遇到错误:尝试在cPanel服务器设置上使用PHP获取Json数据
P粉896751037
P粉896751037 2023-08-13 19:44:52
[PHP讨论组]

我有一个包含javascript的index.html文件:

async function fetchCelebritiesData() {
  try {
    const response = await fetch('/data.php');
    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Error fetching data:', error);
    return [];
  }
}

PHP文件将数据连接到应用程序,并从names.json文件中获取数据。

<?php
// 检查请求是否来自允许的域名
$allowedOrigins = array("example.com", "www.example.com");

$origin = $_SERVER['HTTP_ORIGIN'] ?? ''; // 从请求中获取HTTP_ORIGIN

if (in_array($origin, $allowedOrigins)) {
    header("Access-Control-Allow-Origin: $origin");
} else {
    header("HTTP/1.1 403 Forbidden");
    exit;
}

// 读取并输出JSON数据
$data = file_get_contents("data.json");
header("Content-Type: application/json");
echo $data;
?>

这个设置在控制台中给我一个错误,错误信息如下:

ET https://example.com.com/data.php 403
fetchCelebritiesData @ (index):291
(anonymous) @ (index):375
load (async)
(anonymous) @ (index):373
(index):295 Error fetching data: SyntaxError: Unexpected end of JSON input
    at fetchCelebritiesData ((index):292:33)
    at async (index):375:30
fetchCelebritiesData @ (index):295
await in fetchCelebritiesData (async)
(anonymous) @ (index):375
load (async)
(anonymous) @ (index):373

需要帮助理解可能的问题。我已经检查了PHP和JSON文件以及文件夹的权限,看起来都没问题。--6 4 4--

生成了一个日志,显示了这个错误的重复。

[11-Aug-2023 09:08:58 UTC] PHP Notice: Undefined index: HTTP_ORIGIN in /home/pixellic/public_html/web-applications/celebrities-age-finder/get_secure_data.php on line 4

我是一个编码新手。

谢谢。

我尝试使用php文件安全地获取json数据。

但是我得到了一个403错误。

P粉896751037
P粉896751037

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号