2013-08-28 40 views
3

我正在與jQuery.ajax()一起發送一些HTML表單數據到我的Perl腳本在服務器上,然後返回一些數據回到前端。將數據傳遞給CGI腳本並返回jQuery.ajax

優選地,數據應該是文本/字符串格式。我還需要將它保存爲一個變量,以便在jQuery函數中進一步使用。

我已經設置了HTML代碼,該的JavaScript代碼和CGI的Perl腳本,但在JavaScript的工作,到CGI Perl腳本連接不,我總是得到錯誤信息: 「腳本調用不成功」

也許有人可以告訴我錯誤在哪裏?我並不完全知道如何將Perl腳本中的變量返回給服務器。

由於我還是新來的jQuery和JavaScript,並沒有在異步調用之前,任何幫助將不勝感激。

這是我的代碼:

的HTML代碼:(其中用戶填寫表單用他的名字和名稱:

<html> 
    <head> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script> 
    <script type="text/javascript" src="get_names.js"></script> 
    </head> 
    <body> 
    <div id="loginContent" class="container"> 
     <div id="loginResult" style="display:none;"> 
     </div> 
     <form id="loginForm" name="loginForm" method="post" action=""> 
     <fieldset> 
      <legend>Enter information</legend> 
      <p> 
      <label for="firstname">First name</label> 
      <br /> 
      <input type="text" id="firstname" name="firstname" class="text" size="20" /> 
      </p> 
      <p> 
      <label for="name">Name</label> 
      <br /> 
      <input type="test" id="name" name="name" class="text" size="20" /> 
      </p> 
      <p> 
      <button type="submit" class="button positive"> 
      Login 
      </button> 
      </p> 
     </fieldset> 
     </form> 
    </div> 
    </body> 
</html> 

的JavaScript代碼:

$(document).ready(function(){ 
    $("form#loginForm").submit(function() { // loginForm is submitted 


var firstname = $('#firstname').attr('value'); // get first name 
var name = $('#name').attr('value'); // get name 

if (firstname && name) { // values are not empty 
    $.ajax({ 
     type: "POST", 
     url: "/cgi-bin/perl_script.cgi", // URL of the Perl script 

     // send firstname and name as parameters to the Perl script 
     data: "firstname=" + firstname + "&name=" + name, 

     // script call was *not* successful 
     error: function() { 
      alert("script call was not successful"); 
     }, 

     // script call was successful 
     // perl_data should contain the string returned by the Perl script 
     success: function(perl_data){ 
      alert("Your name is: " + perl_data) 
     } 
    }); 
} 

else { 
    $('div#loginResult').text("Enter your name"); 
    $('div#loginResult').addClass("error"); 
} 

$('div#loginResult').fadeIn(); 
return false; 
    }); 
}); 

Perl代碼:

#!/usr/bin/perl -w 
use CGI; 
use strict; 
use warnings; 

# read the CGI params 
my $cgi = CGI->new; 
my $firstname = $cgi->param("firstname"); 
my $name = $cgi->param("name"); 

my $completename = $firstname . $name; 

print $completename; 

回答

7

問題出在Perl程序中。它不會編譯。它不會發送所需的HTTP標頭。添加print $cgi->header('text/plain;charset=UTF-8');或類似內容,並小心編碼適合內容類型的輸出。

+0

謝謝,它現在有效。我將這個變量轉換爲一個字符串,以便更好地度量並糾正頂部的一個錯字:'my $ completename = $ firstname。 $名稱; my $ completename =「$ completename」; print $ cgi-> header('text/plain; charset = UTF-8'); print $ completename;' – atreju

+0

您的代碼'$ completename =「$ completename」'完全沒有任何用處。爲什麼不只是'打印'$ firstname $ name「;'? –

+0

你說得對,當然。這只是我添加真實Perl代碼之前需要運行的一個簡單示例。 – atreju