[英]How to access using php a json file located in the src folder in ReactJS
How to access using php a json file located in the src folder in ReactJS如何使用 php 访问位于 ReactJS 中 src 文件夹中的 json 文件
my data.json file in my Project我的项目中的 data.json 文件
After building I can't access to the data.json file构建后我无法访问 data.json 文件
The files look like this build folder这些文件看起来像这个构建文件夹
Generally speaking, you don't.一般来说,你不需要。 The src folder contains the source code for the React app. src 文件夹包含 React 应用程序的源代码。 It isn't something that should be directly used in production.它不应该直接用于生产。
If you are using Webpack to build your production version then, as per this answer you can use the CopyWebpackPlugin plugin to copy the JSON file to your build directory and deploy it with the rest of the application.如果您使用 Webpack 构建您的生产版本,那么根据这个答案,您可以使用CopyWebpackPlugin 插件将 JSON 文件复制到您的构建目录,并使用应用程序的 rest 部署它。
That should give you a predictable file path (if the PHP is on the same system as the server for the React frontend) or URL (otherwise) that you can request with file_get_contents
and then parse with json_decode
.这应该给你一个可预测的文件路径(如果 PHP 与 React 前端的服务器在同一个系统上)或 URL(否则)你可以用file_get_contents
请求然后用json_decode
解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.