javascript - 无法获取 JSON 文件
问题描述
package.json 文件:
{
"name": "check",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"dev": "webpack --mode development --watch",
"build": "webpack --mode production"
},
"keywords": [],
"author": "",
"license": "UNLICENSED",
"private": true,
"description": "",
"devDependencies": {
"@babel/core": "^7.2.0",
"@babel/plugin-proposal-class-properties": "^7.2.1",
"@babel/plugin-proposal-object-rest-spread": "^7.2.0",
"@babel/plugin-transform-destructuring": "^7.2.0",
"@babel/preset-env": "^7.2.0",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.4",
"copy-webpack-plugin": "^4.6.0",
"css-loader": "^2.1.0",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"prop-types": "^15.6.1",
"react": "^16.4.0",
"react-dom": "^16.4.0",
"react-redux": "^5.0.7",
"redux": "^4.0.0",
"style-loader": "^0.23.1",
"webpack": "^4.41.2",
"webpack-cli": "^3.0.3",
"webpack-dev-server": "^3.9.0"
},
"dependencies": {
"axios": "^0.18.0",
"babel-polyfill": "^6.26.0",
"file-loader": "^4.2.0",
"jquery": "^3.4.1",
"react-circular-progressbar": "^2.0.1",
"react-google-recaptcha": "^1.0.5",
"react-image": "^2.2.0",
"react-native-progress-circle": "^2.0.1",
"react-router-dom": "^4.3.1",
"redux-thunk": "^2.3.0",
"selectn": "^1.1.2"
}
}
webpack.config,js 文件:
const HtmlWebPackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
module.exports = {
output: {
publicPath: "/"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.html$/,
use: [
{
loader: "html-loader"
}
]
},
{
test: /\.css$/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader"
}
]
},
{
test: /\.mp3$/,
loader: "file-loader"
},
{
test: /\.png$/,
loader: "file-loader"
}
]
},
plugins: [
new HtmlWebPackPlugin({
template: "./src/index.html"
}),
new CopyWebpackPlugin([
{
from: "./api",
to: "./api",
force: true
},
{
from: "./catalogue.pdf",
to: "./catalogue.pdf",
force: true
},
{
from: "./src/smartmenus-1.1.0",
to: "./smartmenus-1.1.0/",
force: true
},
{
from: "./src/resources",
to: "./resources/",
force: true
},
{
from: "./src/favicon.ico",
to: "./favicon.ico",
force: true
}
])
],
devtool: "source-map"
};
settings.php 文件:
?php
define('FILE_PATH', '../files/');
define('JSON_PATH', FILE_PATH . 'json/');
?>
获取.php 文件:
<?php
require_once('settings.php');
$file = preg_replace('/(\.){2,}/', '', $_GET['file']);
$fullPath = 文件路径。$文件;
$mimetype = mime_content_type('../files/'.$file);
if('application/octet-stream' == $mimetype && 'mp3' == end(explode(".", '../files/'.$file))) {
$mimetype = 'audio/mpeg';
}
header("X-Sendfile: $fullPath");
header("Content-type: $mimetype");
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
echo $fullPath;
die();
?>
.htaccess 文件:
<Files get.php>
XSendFile On
</Files>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /api/
#Note: If not(!) match on any of the three conditions below then
# perform the RewriteRule
# !=not, ^/beginning of string, $=end of string
# [L] means do not continue processing this file any further
#We do not want to redirect any files that are a direct link to an image or css file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?q=%{REQUEST_URI}&api=$1 [L,QSA]
</IfModule>
index.php 文件:
<?php
session_start();
require_once('settings.php');
$url = trim($_GET['api'], '/');
$counter = 0;
foreach ($_GET as $key => $value) {
if ($key !== 'q' && $key !== 'api') {
if ($counter === 0) {
$url .= '?';
} else {
$url .= '&';
}
$url .= $key . '=' . $value;
$counter++;
}
}
$splitUrl = explode('/', $url);
$firstSection = (count($splitUrl) > 0) ? $splitUrl[0] : null;
if (!empty($firstSection)
&& in_array($firstSection, [
'main_menu',
'birds',
'birds1',
'Waterfowl',
'Land-Birds',
'fish',
'food',
'food1',
'animal_homes',
'verbs',
'wild_animals',
'buildings',
'bones',
'male_kinship_terms',
'kinship_terms',
'female_kinship_terms',
'sixSeasons',
'trees',
'yearly_calendar',
'syllabic_matching_game1',
'syllabic_matching_game2',
'syllabic_matching_game3',
'syllabic_matching_game4',
'syllabic_matching_game5',
'syllabic_matching_game6',
'syllabic-chart',
'syllabic_chart',
'numbers',
'northern_saskatchewan_place',
'household_items',
'classroom_objects',
'catalog'
])
) {
$file = "temp_debug.txt";
file_put_contents($file, $firstSection);
echo file_get_contents(JSON_PATH . $url . '.json');
}
GET http://localhost:8080/api/main_menu 404(未找到)
main_menu.json 是一个 json 文件。它位于项目根目录的文件夹中。但是所有内容都在没有图像和 json 文件的情况下加载!当我转到链接http://localhost:8080/api/main_menu时,它显示无法获取 /api/main_menu,它应该重定向到http://localhost:8080/api/index.php?api=main_menu并通过 htaccess 获取文件。另一方面,对于图像和音频文件,它就像http://localhost:8080/api/get.php?file=media/Home/bald_eagle.png并且应该获取文件。但它只下载 get.php 文件本身!
解决方案
推荐阅读
- flutter - 在flutter multi_image_picker 4.7.14中访问相机后,iOS 14绿点通知灯始终可见
- python - Catboost:为什么多类分类在内部转换为回归/单类分类问题
- database - 归一化为 4NF?
- python - TypeError:列表索引必须是整数或切片,而不是 Windows 10 上的 str
- python-3.x - 无法插入包含 % 的记录
- arduino - PIR 运动传感器
- java - IntelliJ IDEA 不索引 GraalVM 库
- django-rest-framework - Django-rest-framework filter_queryset() 接受 3 个位置参数,但给出了 4 个
- node.js - 我的随机图像机器人尝试从主文件夹而不是指定文件夹中提取图像
- javascript - 在 Asp.Net C# 中使用 javascript 拆分多行文本框值