JQuery AJAX Perl的问题

I have a pretty simplistic login handler for my website that I am having trouble with, user fills in username and password into appropriate input, ajax sends the information to the perl script that checks, and returns a message on whether it was successful or not and that message is placed into a div. Right now ajax just returns "undefined" when i grab the error message with: XMLHttpRequest.responseText. No message result is sent at all. The status, username and password work properly.

JQuery

<script>
function login() {
        var login = {
            status: "login",
            username: $('#username').val(),
            password: $('#password').val()
            };
            $.ajax({
                    type: 'POST',
                    url: 'bar.cgi',
                    dataType: "json",
                    data: login,
                     },
                    }).done(function( msg ) {
                                                $('#result').html( msg );
                                            });
        }
</script>

Perl bar.cgi

#!/usr/bin/perl

use strict;
use warnings;

use CGI;
use JSON;
use Template;

my $q = CGI->new;
my $dbh = DBI->connect( $db, $db_user, $db_pass ) or die("Connection Error: 
+DBI::errstr
");

my $s = CGI::Session->new("driver:mysql;serializer:storable", $q, {'Handle'=> $dbh }) or 
+die( CGI::Session->errstr );   

if ($q->param('status') eq 'login') {

...check that username/password matches database if so modify session

print $s->header('application/json');
print to_json("login successful");
exit;    
}

to_json is probably failing since you are not passing it a hash and are just passing a string.

$ perl -MJSON -e "print to_json({msg => 'login success'});"
{"msg":"login success"}

$ perl -MJSON -e "print to_json('success');"
hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this) at ...

Install firebug or something similar for whatever browser you are using so you can make sure that your json is returning successfully when the ajax call is made.

Check your error logs for whatever server you are using to see if your perl backend is throwing an error.