iT邦幫忙

0

以ajax的方式更新perl

請教一下各位前輩
目前寫好了一個會抓取資料並print出來的簡單perl
可以如何讓他能以ajax的方式定時刷新?
謝謝~

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

4
wiseguy
iT邦超人 1 級 ‧ 2015-08-22 11:38:11
最佳解答

catding提到:
目前寫好了一個會抓取資料並print出來的簡單perl
可以如何讓他能以ajax的方式定時刷新?

以我開發這麼多年的 Web AP 來看,這兩句話有著很神奇的矛盾。

  1. perl 可以在 console 環境 print 資料,不代表它可以 print 到 web。
  2. 要用 ajax 執行 perl,要有設定好可執行 perl CGI 的 web server。
  3. perl 要能成為 CGI,除了 web runner 可執行它之外,還要遵循 CGI 規格,先輸出 http header。
  4. ajax 是要寫 javascript 去 web server 做 http request,跟 perl 無關。
    以上條件都清楚或是已經備妥了嗎?
catding iT邦新手 5 級 ‧ 2015-09-12 21:41:57 檢舉

感謝回答~
我回去多了解一點來弄

2
老鷹(eagle)
iT邦高手 1 級 ‧ 2015-08-20 12:25:12

我本身沒學過perl不過有找到一個範例,
看對你有沒有用,看起來跟PHP一樣,使用JQuery框架去用.
頁面提出請求與參數,然後後端語言處理傳回,頁面再把資料顯示.

html

<pre class="c" name="code">


    
        <title>Testing ajax</title> 
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


    <script>

            $(document).ready(function() {

                $("#test").click(function(){
                    var ID = 100;
                    $.ajax({
                            type: 'POST',
                            url: '/cgi-bin/ajax/stackCGI/ajax.pl',
                            data: { 'data_id': ID },
                            success: function(res) {

                                                        alert("your ID is: " + res.result);

                                                    },
                            error: function() {alert("did not work");}
                    });
                })

            })



        </script>
    
    

        <button id="test" >Testing</button>

    

ajax.pl

<pre class="c" name="code">
#!/usr/bin/perl

use strict;
use warnings;

use JSON; #if not already installed, just run "cpan JSON"
use CGI;

my $cgi = CGI->new;

print $cgi->header('application/json;charset=UTF-8');

my $id = $cgi->param('data_id');    

#convert  data to JSON
my $op = JSON -> new -> utf8 -> pretty(1);
my $json = $op -> encode({
    result => $id
});
print $json;

來源請點這邊

我要發表回答

立即登入回答