+ -
当前位置:首页 → 问答吧 → perl jquery getjson问题

perl jquery getjson问题

时间:2009-08-29

来源:互联网

按照网上的例子稍微修改了一下,但是不成功,没有返回结果显示,初次接触,请高手指点。注:perl的json模块已安装
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>Perl, jQuery and JSON</title>

<script type="text/javascript" src="jquery-1.3.2.js"></script>

</head>

<body>


<div id="use_ajax">

      <p>This form uses jQuery. The submit will send a GET AJAX request and
      will return a JSON object which we can use to update the div.
      </p>

      <button id="submit_form_ajax">Get User Details</button>
      <span id="kk" style="display:none;">   loading...</span>

      <div id="display_user_details">

      </div>
</div>

<script type="text/javascript">

// show the spinner for AJAX requests
$("#kk").ajaxStart(function(){
    $(this).show();
});


$("#kk").ajaxStop(function(){
    $(this).hide();
});

$("#submit_form_ajax").click(function(){
    $.getJSON("test.pl",function(json){
        $("#display_user_details").html(
            '<h3>User Details</h3>'
            + 'Name: ' + json.first_name + ' ' + json.last_name + '<br/>'
            + 'Sex: ' + json.sex + '<br/>'
            + 'Age: ' + json.age + '<br/>'
        );
    });
});


</script>

</body>
</html>





vi test.pl

#!/usr/bin/perl
use strict;
use warnings;

use JSON;
use CGI;

my $q  = CGI->new;


my $user_details = get_user_details();
my $json = to_json($user_details);
print $q->header('application/json');
print $json;

sub get_user_details
{

    my $user_details = {
        first_name => 'Bob',
        last_name  => 'Green',
        sex        => 'M',
        age        => 35,
    };
  return $user_details;
}

作者: since   发布时间: 2009-08-29

执行./test.pl  , 输出以下结果:

{"sex":"M","age":35,"first_name":"Bob","last_name":"Green"}

作者: since   发布时间: 2009-08-29

顶一下!

作者: since   发布时间: 2009-08-30

终于弄明白了,test.pl没有放到cgi-bin目录里面。

作者: since   发布时间: 2009-08-30