簡體   English   中英

試圖了解CGI和AJAX的工作原理-為什么此演示無效?

[英]Trying to understand how CGI and AJAX work — why isn't this demo working?

因此,我試圖了解所有這些工作原理,並找到了一個很棒的小演示。

我的Web服務器的cgi-bin目錄中有一個名為simple-ajax-example.cgi的CGI腳本。

#!C:\strawberry\perl\bin\perl.exe
use CGI;

$query = new CGI;

$secretword = $query->param('w');
$remotehost = $query->remote_host();

print $query->header;
print "<p>The secret word is <b>$secretword</b> and your IP is <b>$remotehost</b>.</p>";

我還有一個.html文件,名為simpleAJAX.html(據我了解,與我命名此文件無關)。 我不確定將其放置在何處,因此也將其放在cgi-bin目錄中。

<html>
<head>
<title>Simple Ajax Example</title>
<script language="Javascript">
function xmlhttpPost(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(getquerystring());
}

function getquerystring() {
    var form     = document.forms['f1'];
    var word = form.word.value;
    qstr = 'w=' + escape(word);  // NOTE: no '?' before querystring
    return qstr;
}

function updatepage(str){
    document.getElementById("result").innerHTML = str;
}
</script>
</head>
<body>
<form name="f1">
  <p>word: <input name="word" type="text">  
  <input value="Go" type="button" onclick='JavaScript:xmlhttpPost("/cgi-bin/simple-ajax-example.cgi")'></p>
  <div id="result"></div>
</form>
</body>
</html>

我在瀏覽器中打開兩個選項卡,導航到兩個文件。 一切順利,直到我去更新simpleAJAX.html中的字段。 我鍵入“ Hello”,然后單擊“執行”,但是在瀏覽器中的simple-ajax-example.cgi中該詞未更新。 有任何想法嗎? 我正在從中學習的演示位於: http : //www.degraeve.com/reference/simple-ajax-example.php

Perl cgi是100%可以html ajax 100%可以

所以一步一步調試

1,單擊按鈕時,cgi腳本會執行嗎? 是/否方法檢查(例如,在cgi中放置一些行以創建文件),如果否,則檢查“ /cgi-bin/simple-ajax-example.cgi”是否是您的cgi的正確路徑以及權限

是的,我的權限設置為可執行。 我將其添加到我的httpd.conf文件中

不,它是chmod 755 simple-ajax-example.cgi用於將可執行文件權限設置為文件的命令,它與httpd.conf無關,它實際上是服務器本身的少數配置文件之一

如果是,則繼續執行代碼並檢查返回值,但我確定答案是您的cgi無法執行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM