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

标签 javascript html ajax perl apache

我正在使用 AJAX 和基于 Web 的服务器 [APACHE] 来调用 Perl 脚本。

我的文件位于 htdocs 中,我的服务器在其中访问这些文件。当我单击 test.html 时,它会弹出一个按钮“test”并成功调用 perl 脚本来简单地打印出一条消息。即,perl 脚本仅打印“helloworld”,并且 html 文件“警告”用户,即按下按钮时打印出“hello world”。这很好用。

问题是,我想要做的是调用一个perl脚本“check.pl”,其中check.pl打开一个文本文件“simple.txt”,将该文本文件的内容存储在一个字符串中,然后打印结果。因此,通过按下 test.html 生成的按钮,它应该打印出文本文件的内容。现在 simple.txt 只是一句话。

这是我的 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>

这是它调用的 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);
<小时/>

我是 Perl、AJAX、HTML 和 javascript 新手。问题是当我按下按钮时,什么也没有出现。事实上,“simple.txt”的内容应该提醒用户。我查看了错误日志文件,它显示“无法打开 simple.txt,文件或目录不存在”。不过,正如我之前所说,我的所有三个文件都在 htdocs 中。这里可能出现什么问题?

最佳答案

我怀疑您的 Perl 脚本的当前工作目录与 htdocs 不同。您应该使用其路径完全限定文件名。

另外:

  • 您应该始终每个 Perl 程序使用严格使用警告

  • 如前所述,#! 行必须是文件中的第一行

  • 当以下数据是简单文本时,您是在告诉客户端以下数据是 HTML。

  • 您应该将 open 的三参数与词法文件句柄一起使用。

您的程序的本次更新考虑了这些要点

#!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;

关于javascript - 通过AJAX调用Perl脚本打印文本文件的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16266408/

相关文章:

javascript - 从sqlite数据库读取信息,语法?如何在 html5 webapp 中使用它?

jquery - AJAX:获取html并检查图像是否已完成加载

javascript - 使用按钮将本地 JavaScript 变量传递到 Phaser 游戏上的全局变量

javascript - ul 样式仅在滚动时更改,返回滚动/更改点时会丢失样式

javascript - 如何在javascript中基于相同的键和值合并两个对象数组?

html - 内容超出带有粘性页脚的页面

javascript - 文字不会出现在 IE8 及更低版本中

php - 如何从 Laravel 中的 Ajax 调用中检索数据?

jquery - MySQL - 3 个不同的表具有相同的列连接在 1 列中

javascript - 模块 - JavaScript : Good Parts. 此回调如何获取其参数?