简体   繁体   English

外部URL链接

[英]External URL linking

I have the following code: 我有以下代码:

<script>
        var fileContent;
        $.ajax({
            url : "text.txt",
            dataType: "text",
        ...

It loads the text from a .txt file in order to obtain the data. 它从.txt文件加载文本以获取数据。 If the text.txt is on the same path as the html code, it loads the data. 如果text.txt与html代码位于同一路径上,则它将加载数据。 However, if I type for example (placing the file in a different folder): 但是,如果我输入例如(将文件放置在其他文件夹中):

url: "../../../files/text.txt"

It does not allow me to obtain the file. 它不允许我获取文件。 Any ideas of how to do it or how to implement it without changing the code in a significant way? 在不以重大方式更改代码的情况下如何实现或实现它的任何想法? Thanks! 谢谢!

There are three possible causes of this: 有三个可能的原因:

You are using HTTP 您正在使用HTTP

You are using HTTP and the path you are trying to access is not exposed by your web server. 您正在使用HTTP,并且您尝试访问的路径不会被Web服务器公开。 (You cannot access files above the directory root by default). (默认情况下,您无法访问目录根目录之上的文件)。

You need to give the file a URL on your web server and request that URL. 您需要为文件提供Web服务器上的URL,然后请求该URL。

You are using local files 您正在使用本地文件

Different browsers have different security restrictions for Ajax on local files. 不同的浏览器对本地文件上的Ajax具有不同的安全限制。

You can get this problem if the file is in a directory above the HTML document (some browsers only allow you to access files in the same or a lower directory). 如果文件位于HTML文档上方的目录中,则可能会出现此问题(某些浏览器仅允许您访问同一目录或更低目录中的文件)。

You can resolve this by using HTTP. 您可以使用HTTP解决此问题。 Ajax in general works very poorly without HTTP. 通常,如果没有HTTP,则Ajax的效果会很差。

You simply have the URL wrong 您输入的网址错误

Correct the URL. 改正URL。

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

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