繁体   English   中英

为什么 XMLHTTP 请求不适用于我的服务器?

[英]Why does the XMLHTTP Request doesn't work with my server?

我租了一台服务器,现在有一个网站。 但是我有一个问题:我想引用我保存在服务器上的文本文件并将内容加载到字符串中。 我尝试了以下方法:

 <script type="text/javascript"> var filePath = "the link"; xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET",filePath,false); xmlhttp.send(null); var fileContent = xmlhttp.responseText; var fileArray = fileContent.split('\\n') //Now do whatever you need with the array window.alert(fileArray); </script>

问题是:我不工作:-(我可以加载其他文本文件(例如从 GitHub)但是在我的服务器上引用文本文件经常无法工作!这些文件是公开可用的,可以被读取和执行。我也试图得到通过Java的内容:

 import java.net.*; import java.io.*; public class URLReader { public static void main(String[] args) throws Exception { URL oracle = new URL("the link"); BufferedReader in = new BufferedReader( new InputStreamReader(oracle.openStream())); String inputLine; System.out.println(in.readLine()); while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); } }

这段代码工作正常(即使对于我的服务器文件)。 你能帮助我吗? 为什么它不起作用?

cors已经假设的那样,这很可能是cors问题。 您可以通过向服务器上的.htaccess添加选项Header always set Access-Control-Allow-Origin: "*"来解决此问题。

暂无
暂无

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

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