简体   繁体   English

使用jQuery从绝对路径的外部文件加载HTML

[英]Use jQuery to load HTML from external file at absolute path

I am trying to load the content of an external HTML file into my current HTML template. 我正在尝试将外部HTML文件的内容加载到当前的HTML模板中。

The problem I'm running into is that the content I want to load is not local, and it's not at a URL. 我遇到的问题是我要加载的内容不是本地的,并且不在URL上。 The file is on a central storage directory that I can access while connected to a VPN. 该文件位于连接到VPN时可以访问的中央存储目录中。 In other words, if all my local files are within /Users/username/... , then this file is at /central_directory/absolute/path/to/file.html . 换句话说,如果我所有的本地文件都在/Users/username/... ,则此文件位于/central_directory/absolute/path/to/file.html

I can access the file at /central_directory/absolute/path/to/file.html from the Terminal or at file:///central_directory/absolute/path/to/file.html from within my browser. 我可以从终端在/central_directory/absolute/path/to/file.html或从浏览器中访问file:///central_directory/absolute/path/to/file.html

However, when I try to load the file using the jQuery .load() function, I get a 404 not found error: 但是,当我尝试使用jQuery .load()函数加载文件时,出现404未找到错误:

Not Found: /central_directory/absolute/path/to/file.html 找不到:/central_directory/absolute/path/to/file.html

[15/Jul/2018 19:04:22] "GET /central_directory/absolute/path/to/file.html HTTP/1.1" 404 2257 [15 / Jul / 2018 19:04:22]“ GET /central_directory/absolute/path/to/file.html HTTP / 1.1” 404 2257

My guess is that it's not looking in the right place for the file (eg it is looking at http://mywebsite/central_directory/absolute/path/to/file.html ), but I don't know if this is an accurate guess, or how to test. 我的猜测是它没有在正确的位置查找文件(例如,它正在查找http://mywebsite/central_directory/absolute/path/to/file.html ),但是我不知道这是否准确猜测,或如何测试。


Code: 码:

Here is the jQuery script, in the head of my current template: 这是jQuery脚本,位于我当前模板的head

<script>
    $(function(){
        var filename = "file:///central_directory/absolute/path/to/file.html"
        $("#includedContent").load(filename);
    });
</script>

and the HTML in the template: 以及模板中的HTML:

<div id="includedContent"></div>

Simple enough, right? 很简单吧?

I have also tried other syntax, with the same 404 error occurring. 我还尝试了其他语法,发生了相同的404错误。

The problem is that .load() requires an HTTP connection (so a GET or POST request) for javascript. 问题是.load() 需要 javascript的HTTP连接(因此是GETPOST请求)。

This is the fortunate result of the fact that you cannot load a javascript resource in the same way you can navigate to a file:// path in your browser or terminal ('fortunate' because it prevents anybody from accessing any file on your computer). 这是由于您无法以与浏览器或终端中的file://路径相同的方式加载javascript资源的事实(这是“幸运的”,因为它阻止任何人访问您计算机上的任何文件),这是一个幸运的结果。 。

The only real way to get around this is by having the local file proxied through a web server to your browser (so you'll need to have control over the server implementation). 解决此问题的唯一真正方法是通过Web服务器将本地文件代理到浏览器(因此您需要控制服务器的实现)。 If you have an existing web server stack (like PHP , Node.js , Ruby , etc) you can very likely do this, but it ultimately depends on what it is. 如果您有一个现有的Web服务器堆栈(如PHPNode.jsRuby等),则很可能会这样做,但最终取决于它是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM