gpt4 book ai didi

Javascript 与 HTML 位于不同的文件夹中

转载 作者:行者123 更新时间:2023-11-28 12:13:30 25 4
gpt4 key购买 nike

我是网络开发新手。我在将文件存储在不同的文件夹中时遇到问题(出于安全原因)。这是返回错误的索引页

<head>
<script src="../scripts/script.js"></script>
</head>
<body>
<?php include "../php/page.php"; ?>

Other code here...
</body>

我的文件结构如下

www/
html/
index.php

scripts/
script.js

php/
page.php

我不明白为什么包含 php 文件可以工作(提供的示例代码中的第 5 行),而包含 javascript 则不行(第 2 行)。我猜您对该错误感兴趣,因此 Google Chrome 的控制台显示如下

Failed to load resource: the server responded with a status of 404 (Not Found)

它还显示了指向资源的链接,并且似乎在查找 my.server.address/scripts/script.js ,就像它不关心 ../部分一样。有人可以解释这种行为吗?

最佳答案

PHP 解析计算机文件系统上的路径。

Web 浏览器解析 URL 上的路径。

您的 HTTP 服务器有 http://my.server.address/ 映射到文件系统上的 www/html

因此 ../scripts/script.js/ ... 上升一级到 / (因为这是 top),然后向下到 /scripts,然后向下到 /scripts/script.js

浏览器向 HTTP 服务器请求 /scripts/script.js 并将其映射到文件系统 - www/html/scripts/script.js 并返回404 因为该文件不存在。

浏览器只能读取 Web 服务器将发送给它的数据,并且通过将 scripts 目录保留在文档根目录之外,您将无法通过 Web 服务器使其可用。

关于Javascript 与 HTML 位于不同的文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55403361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com