从Mac服务器中的HTML运行CGI脚本



i有以下代码位于 /Library/WebServer/Documents/ and cgi code in /Library/WebServer/Documents/cgi-bin/bot_stop.cgi

html代码:

<button style="height: 75px; width: 85px" onclick="bot_stop()">
    <img style="height: 63px"src="http://images.clipartpanda.com/stop-sign-clipart-119498958977780800stop_sign_right_font_mig_.svg.hi.png">
</button>

XMP代码:

function bot_stop()
{
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET","cgi-bin/bot_stop.cgi",true);
    xmlhttp.send();
    alert("I am an alert box!");
}

CGI代码:

#!/bin/bash
echo hello world

我怎么知道脚本运行?我也收到弹出窗口,但知道了上述功能有效的方法。

您的咬伤超出了一次咀嚼。我会尽力帮助您的第一部分。

对于故障射击,最好降低复杂性。与其在浏览器中制作XHR,不如首先在命令行中提出HTTP请求,这是一个更简单的问题。

安装curl。Homebrew有最新版本。运行curl -v http://localhost/cgi-bin/bot_stop.cgi

如果您拒绝连接,则Web服务器未运行或在不同的端口上运行,它重新启动它或查看Web服务器配置配置已配置的端口是什么。例如,如果是8080,则需要运行curl -v http://localhost:8080/cgi-bin/bot_stop.cgi

如果未发现错误或"系统找不到指定的文件",则无法正确配置Web服务器从该位置运行CGI程序。阅读Web服务器文档如何更改配置以从位置/Library/WebServer/Documents/cgi-bin/运行CGI程序。

如果您遇到了内部服务器错误,请查看Web服务器错误日志。如果说"不能执行:拒绝权限",则需要在CGI程序上设置可执行文件属性。如果说"脚本标头的过早结束",则CGI程序可以运行,但没有返回正确的HTTP响应。由于您使用Bash,因此您需要自己构造HTTP标头:

#!/bin/bash
echo Content-Type: text/plain
echo
echo hello world

使用Perl,CGI程序看起来很像:

#!/usr/bin/env plackup
use strict;
use warnings;
my $app = sub {
    return [
        200,
        ['Content-Type' => 'text/plain'],
        ['hello world'],
    ];
};

成功的卷曲响应应看起来与:

< HTTP/1.1 200 OK
< Date: Tue, 13 Feb 2018 09:03:25 GMT
< Server: blahblahblahblah
< Content-Length: 12
< Content-Type: text/plain
<
hello world

现在您知道了这一点,可以在浏览器地址栏中使用URL,然后尝试通过按钮触发XHR。使用开发人员工具/JavaScript控制台检查错误详细信息。

最新更新