简体   繁体   English

无法从服务器 .txt 文件中获取文件内容

[英]can't get file content from server .txt file

I'm working in dhtml (html5, CSS3, latest JavaScript, and Perl 5.26.3) using an Apache/2.4.39 (Win64) and mod_perl/2.0.10 server on Windows 10. I don't seem to be able to get ajax to work.我正在 Windows 10 上使用 Apache/2.4.39 (Win64) 和 mod_perl/2.0.10 服务器处理 dhtml(html5、CSS3、最新的 JavaScript 和 Perl 5.26.3)。我似乎无法让阿贾克斯工作。

I'm trying to 'get' a multi-line file (directory and file name) out of a database location.我正在尝试从数据库位置“获取”一个多行文件(目录和文件名)。 I'm thinking that the response text will return a string of lines separated by line ends (\\n).我认为响应文本将返回一串由行尾 (\\n) 分隔的行。 I've tried the w3 simplest approach, jquery, and d3.我已经尝试过 w3 最简单​​的方法、jquery 和 d3。 The w3 approach is below. w3 方法如下。

    var mFn = document.forms[0]['thefdir'].value + 'Measures/all.mea?t=' + Math.random(); // Unique ID bypasses cache.

    var xhttp = new XMLHttpRequest();
    xhttp.open('GET', 'dog.txt', true);
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        fieldName3 = this.responseText;
      }
    };    
    xhttp.send();

Its executed onChange to a field on a form generated by a Perl CGI program.它对 Perl CGI 程序生成的表单上的字段执行 onChange。 The fieldName3.value would be processed downstream in the JS. fieldName3.value 将在 JS 中进行下游处理。

I've tried it with the fully qualified location (mFn) and a relative location (dog.txt) with 1 line in it.我已经尝试过使用完全限定位置 (mFn) 和其中包含 1 行的相对位置 (dog.txt)。 Neither worked.都没有工作。 I don't know what directory the relative location resolves to.我不知道相对位置解析到哪个目录。 So I put a copy in the server root and the cgi-bin directories.所以我在服务器根目录和 cgi-bin 目录中放了一个副本。 It did not work.这没用。 Nothing happens and no error information is generated.什么也没有发生,也不会产生错误信息。

Any help will be appreciated.任何帮助将不胜感激。 craigt克雷格

The data in the console and finally in an alert looks like a multi-line file where each line ends in a CRLF, as verified by Notepad++.控制台中的数据和最后在警报中的数据看起来像一个多行文件,其中每一行都以 CRLF 结尾,正如 Notepad++ 所验证的那样。 This is what the file on the server is.这是服务器上的文件。

Apparently, with the asynchronous request, the downstream javascript gets executed before the request from the server completes.显然,对于异步请求,下游 javascript 会在来自服务器的请求完成之前执行。 When I change it to a synchronous request it worked as hoped in all 5 browsers.当我将其更改为同步请求时,它在所有 5 个浏览器中都按预期工作。 So I ether have to 1) be able to make the JS process wait until the onreadystatechange assignment has completed or 2) accept the synchronous execution.所以我必须 1) 能够让 JS 进程等到 onreadystatechange 分配完成或 2) 接受同步执行。 I have read several cautions about 2. The file is small, the activity is aperiodic and increasingly infrequent, and the synchronous effect will be very brief.看了几篇关于2的注意事项。文件小,活动不定期,越来越少,同步效果会很短暂。 What do you think about 1 or 2.你怎么看1或2。

And thanks for working with me.感谢您与我合作。 I apologize for opening this when I did.我为我打开它而道歉。 I could have done a lot more first.我本来可以先做更多。

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

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