2014-06-18 125 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