2014-06-18 49 views
0

使用我有一個HTML表是這樣的:檢索JavaScript變量在Perl

<table class="notices-table" width="100%" cellpadding="0" cellspacing="0" border="0"> 
     <% foreach my $notice (@regular_posts) { %> 
     <form method = "post" id = "post-form-<%= $notice->notice_id()%>"> 
     <tr class = "click-post" href = "<%= $notice->link() %>"> 
      <td><b>Date Posted: </b> <%= '' . (split(/\./, $notice->created()))[0] %><br/> 
      <b>Title: </b><%= $notice->title() %><br/><br/> 
      <%= $notice->content() %> 
      </td> 
      <td><button class = "archive-button" id="archive-button">Archive</button></td> 
     </tr> 
     <input type="hidden" id="action" name="action" value="archive" /> 
     </form> 
     <% } %> 
</table> 

,然後我有觸發時,「存檔」按鈕被擊中的每個atempts檢索JavaScript代碼「notice_id」:

$(document).ready(function() { 
$('button.archive-button').bind('click', function() { 
    var id = $(this).attr('id'); 
    var parts = id.split(/-/); 
    var notice_id = parts[2]; 
    var post_form_id = '#post-form-' + notice_id; 

    $(post_form_id).submit(); 

    var path_name = window.location.pathname; 
    $.ajax(path_name, { 
     type: 'POST' 
     data: { 
      'notice_id2' : notice_id 
     }, 
     success: function(data) { 
      console.log('/takeNotice called successfully:'); 
      console.log(data); 
     }, 
     error: function(jqXHR, textStatus, err) { 
      console.error('/takeNotice call failed:'); 
      console.error(err); 
     } 
    }); 

    return false; 
}); 

});

我想通過「notice_id」成一個Perl腳本,將使用id來檢索數據庫中的正確信息,這樣它會存檔:

my $cgi = CGI->new; 
my $notice_id = $cgi->param("notice_id2"); 
my $form = $Request->Params(); 
my $notice; 
eval { 
    $notice = Taskman::Notice->new(notice_id => $notice_id); 
}; 

我很新成Perl和Javascript/Ajax,所以我可能在這裏有更多的問題。任何見解都會很棒。由於

EDITED 我做了一些修改,我的代碼。阿賈克斯似乎工作,但我不能告訴我是否仍然援引正確與否。這些腳本位於同一頁面上。我仍在檢索錯誤的「notice_id」

+0

你需要設置網址:[看看這裏的文檔](http://api.jquery.com/jquery.ajax/) –

+0

它雖然是相同的頁面。我以爲你可以留下空白,如果它是相同的頁面 – user3752957

回答

0

您的方法沒有任何根本性錯誤,但您沒有正確調用$.ajax,這將阻止任何工作。

具體來說,您沒有提供Perl腳本的URL。爲了舉例,我假設你的Perl腳本生存在路徑/takeNotice(巧妙,不是?)。你會這樣稱呼它:

$.ajax('/takeNotice', { 
    type: 'POST', 
    data: { 
     notice_id2 : notice_id 
    }, 
    success: function(data) { 
     console.log('/takeNotice called successfully:'); 
     console.log(data); 
    }, 
    error: function(jqXHR, textStatus, err) { 
     console.error('/takeNotice call failed:'); 
     console.error(err); 
    } 
}); 

successerror功能是不是絕對必要的,但它是一個好主意,包括他們,當你學習的肯定,它確實有助於瞭解您的面前流 - 結束邏輯。

說到這,你應該還添加一些錯誤處理這樣的代碼:

var parts = id.split(/-/); 
var notice_id = parts[2]; 
var post_form_id = '#post-form-' + notice_id; 

如果ID是不是你希望它是格式,你將最終"#post-form-undefinedpost_form_id 。一些錯誤處理和控制檯日誌記錄在這裏很有用。

最後,此行不執行任何操作:

$(post_form_id '#action').val('archive'); 

如果你想設置的值,則需要第二個參數。如果你想獲得價值,你應該把它分配給某個東西。

+0

我讀的地方,你不需要指定一個URL,如果兩者在同一頁面上。那是錯誤的嗎? – user3752957

+0

啊,不,那是真的。我不一定認爲這是一個好主意,但它會起作用。當然,您將不得不讓服務器端代碼區分'GET'和'POST'請求​​。 –

+0

感謝您的見解。我添加了一些您向我展示的內容以及通過「window.location.pathname;」檢索到的網址並更新了我的代碼,但它仍然沒有檢索到正確的信息。 – user3752957