简体   繁体   English

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

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

I've rented a server and have a website right now.我租了一台服务器,现在有一个网站。 But I have a problem: I want to refer Textfiles I've saved on the server and load the content into a String.但是我有一个问题:我想引用我保存在服务器上的文本文件并将内容加载到字符串中。 I've tried the following:我尝试了以下方法:

 <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>

The problem ist: I doesn't work :-( I can load other Textfiles (eg from GitHub) but referencing Textfiles on my server constantly fails to work! The files are publically available and can be read and executed. I also tried to get the content via Java:问题是:我不工作:-(我可以加载其他文本文件(例如从 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(); } }

This code works fine (even for my server files).这段代码工作正常(即使对于我的服务器文件)。 Can you help me?你能帮助我吗? Why isn't it working?为什么它不起作用?

It's most likely a cors issue as @Alex already assumed. cors已经假设的那样,这很可能是cors问题。 You can fix this by adding the option Header always set Access-Control-Allow-Origin: "*" to your .htaccess on the server.您可以通过向服务器上的.htaccess添加选项Header always set Access-Control-Allow-Origin: "*"来解决此问题。

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

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