简体   繁体   English

通过AJAX调用Perl脚本以打印文本文件的内容

[英]Calling Perl Script through AJAX to print the contents of a text file

I am using AJAX and a web-based server [APACHE] to call a perl script. 我正在使用AJAX和基于Web的服务器[APACHE]来调用perl脚本。

I have my files in htdocs, where my server accesses these files. 我将文件保存在htdocs中,我的服务器在其中访问这些文件。 When I click on test.html it pops up a button "test" and successfully calls a perl script to simply print out a message. 当我单击test.html时,它将弹出一个按钮“ test”,并成功调用perl脚本以简单地打印出一条消息。 ie the perl script simply prints "helloworld" and the html file "alerts" the user, ie prints out "hello world" when the button is pressed. 即,perl脚本仅打印“ helloworld”,而html文件“提醒”用户,即,当按下按钮时,打印出“ hello world”。 This works fine. 这很好。

The problem is, what I want to do, is call a perl script "check.pl", where check.pl opens a text file "simple.txt", stores the content of this text file in a string and then prints the result. 问题是,我想做的是调用一个perl脚本“ check.pl”,其中check.pl打开一个文本文件“ simple.txt”,将该文本文件的内容存储在一个字符串中,然后打印结果。 So by pressing the button that is generated by test.html, it should print out the contents of the text file. 因此,通过按下test.html生成的按钮,它应该打印出文本文件的内容。 Right now simple.txt is simply one sentence. 现在,simple.txt仅是一句话。

Here is my HTML, it successfully executes a perl file [check.pl]: 这是我的HTML,它成功执行了一个perl文件[check.pl]:


<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc() {

//create a variable that will reference the XMLHttpRequest we will create
var xmlhttp;


//****Want it compatible with all browsers*****
//try to create the object in microsoft and non-microsoft browsers
if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
} else {
    // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

//set the event to be a function that executes
xmlhttp.onreadystatechange=function() {
    var a;
    //when server is ready, go ahead
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    //get number from text file, add one to it and output 
    //the original number and the resulting number.
    a = xmlhttp.responseText;
    alert(a);
    }
}
//execute perl script
xmlhttp.open("GET","check.pl",false);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<Apache2.2>/<check.pl>?fileName=<simple.txt>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>

and here is the perl script it calls: 这是它调用的perl脚本:


#test to see if we can open file and print its contents

#The following two lines are necessary!
#!C:\indigoampp\perl-5.12.1\bin\perl.exe
print "Content-type: text/html\n\n";

#This line allows the entire file to be read not just the first paragraph.
local $/;

#Open file that contains the source text to work with
open(FILESOURCE, "simple.txt") or die("Unable to open requested file: simple.txt :$!");

#Store the whole text from the file into a string
my $document = <FILESOURCE>;
print $document;
close (FILESOURCE);

I am new to perl, AJAX, HTML, and javascript. 我是perl,AJAX,HTML和javascript的新手。 The issue is that when I press the button, nothing appears. 问题是当我按下按钮时,什么也没出现。 When in fact, the contents of "simple.txt" should be alerted to the user. 实际上,应该将“ simple.txt”的内容提醒用户。 I looked at the error log file and it says "cannot open simple.txt, the file or directory does not exist". 我查看了错误日志文件,并显示“无法打开simple.txt,文件或目录不存在”。 Though, as I stated before, all three of my files are in htdocs. 但是,正如我之前所说,我所有的三个文件都在htdocs中。 What could be the issue here? 这里可能是什么问题?

I suspect the current working directory for your Perl script is different from htdocs . 我怀疑您的Perl脚本的当前工作目录与htdocs不同。 You should fully-qualify the filename with its path. 您应该使用路径来完全限定文件名。

Also: 也:

  • You should always use strict and use warnings for every Perl program 您应该始终每个 Perl程序use strict use warningsuse warnings

  • As has been commented, the #! 正如已经评论过的, #! line must be the first line in the file 行必须是文件中的第一行

  • You are telling the client that the following data is HTML when it is simple text. 您是在告诉客户以下数据为HTML时是纯文本。

  • You should use the three-parameter for of open with lexical file handles. 您应该使用带有词法文件句柄的open的三个参数。

This update of your program takes those points into account 程序的此更新考虑了这些要点

#!C:\indigoampp\perl-5.12.1\bin\perl.exe

use strict;
use warnings;

my $filename = 'simple.txt';

open my $source, '<', 'C:\path\to\htdocs\\'.$filename
        or die qq{Unable to open requested file "$filename": $!};

my @document = <$source>;
print "Content-type: text/plain\n\n";
print @document;

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

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