Javascript 将JSON字符串传递给Perl CGI

Javascript 将JSON字符串传递给Perl CGI,javascript,jquery,mysql,json,perl,Javascript,Jquery,Mysql,Json,Perl,我试图创建一个登录环境,让用户输入他们的凭据,然后使用JS(使用jquery、ajax和Perl)检查MySQL数据库(如果凭据错误,则返回相应的错误),成功登录后,将为用户显示动态页面(用户门户) 我无法将用户ID传递到最后一个cgi页面 以下是我的代码: login.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>l

我试图创建一个登录环境,让用户输入他们的凭据,然后使用JS(使用jqueryajaxPerl)检查MySQL数据库(如果凭据错误,则返回相应的错误),成功登录后,将为用户显示动态页面(用户门户)

我无法将用户ID传递到最后一个cgi页面

以下是我的代码:

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>login</title>
    <link rel="stylesheet" type="text/css" media="screen, projection" href="http://www.blueprintcss.org/blueprint/screen.css" />
    <link rel="stylesheet"  type="text/css" media="screen, projection" href="http://www.blueprintcss.org/blueprint/plugins/buttons/screen.css" />
    <link rel="stylesheet" type="text/css" media="print" href="http://www.blueprintcss.org/blueprint/print.css" />
    <!--[if IE]><link rel="stylesheet" type="text/css" media="screen, projection" 
        href="http://www.blueprintcss.org/blueprint/ie.css"><![endif]-->
    <script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="login.js"></script>
    <style type="text/css">
    #loginContent { width: 350px; margin: 100px auto; }
    button[type] { margin: 0.5em 0; }
    #loginForm{
        width:25%;
        padding: 5px 5px;
        margin:auto;
    }
    </style>
</head>
<body>
    <h2>Please have pop-ups enabled for this site</h2>
    <div id="loginResult" style="display:none;"></div>
    <form id="loginForm" name="loginForm" method="post" action="">
        <fieldset>
        <legend>Enter Information</legend>
            <p>
                <label for="username">Username</label>
                <br />
                <input type="text" id="username" name="username" class="text" size="20" />
            </p>
            <p>
                <label for="password">Password</label>
                <br />
                <input type="password" id="password" name="password" class="text" size="20" />
            </p>
            <p>
                <button type="submit" class="button positive">Login</button>
            </p>
        </fieldset>
    </form>
</body>
</html>
#!C:\Strawberry\perl\bin\perl.exe
use CGI;
use DBI;
use strict;
use warnings;

# read the CGI params
my $cgi = CGI->new;
my $username = $cgi->param("username");
my $password = $cgi->param("password");

# connect to the database
my $db = 'bakery_users';
my $host = 'localhost';
my $user = 'root';
my $pass = 'P@ssw0rd';
my $dbh   = DBI->connect ("DBI:mysql:database=$db:host=$host", $user, $pass) or die "Can't connect to database: $DBI::errstr\n";
# check the username and password in the database
my $statement = qq{SELECT id FROM users WHERE username=? and password=?};
my $sth = $dbh->prepare($statement)
  or die $dbh->errstr;
$sth->execute($username, $password)
  or die $sth->errstr;
my ($userID) = $sth->fetchrow_array;

# create a JSON string according to the database result
my $json = ($userID) ? 
  qq{{"success" : "login is successful", "userid" : "$userID"}} : 
  qq{{"error" : "username or password is wrong"}};

# return JSON string
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;
login.pl

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>login</title>
    <link rel="stylesheet" type="text/css" media="screen, projection" href="http://www.blueprintcss.org/blueprint/screen.css" />
    <link rel="stylesheet"  type="text/css" media="screen, projection" href="http://www.blueprintcss.org/blueprint/plugins/buttons/screen.css" />
    <link rel="stylesheet" type="text/css" media="print" href="http://www.blueprintcss.org/blueprint/print.css" />
    <!--[if IE]><link rel="stylesheet" type="text/css" media="screen, projection" 
        href="http://www.blueprintcss.org/blueprint/ie.css"><![endif]-->
    <script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="login.js"></script>
    <style type="text/css">
    #loginContent { width: 350px; margin: 100px auto; }
    button[type] { margin: 0.5em 0; }
    #loginForm{
        width:25%;
        padding: 5px 5px;
        margin:auto;
    }
    </style>
</head>
<body>
    <h2>Please have pop-ups enabled for this site</h2>
    <div id="loginResult" style="display:none;"></div>
    <form id="loginForm" name="loginForm" method="post" action="">
        <fieldset>
        <legend>Enter Information</legend>
            <p>
                <label for="username">Username</label>
                <br />
                <input type="text" id="username" name="username" class="text" size="20" />
            </p>
            <p>
                <label for="password">Password</label>
                <br />
                <input type="password" id="password" name="password" class="text" size="20" />
            </p>
            <p>
                <button type="submit" class="button positive">Login</button>
            </p>
        </fieldset>
    </form>
</body>
</html>
#!C:\Strawberry\perl\bin\perl.exe
use CGI;
use DBI;
use strict;
use warnings;

# read the CGI params
my $cgi = CGI->new;
my $username = $cgi->param("username");
my $password = $cgi->param("password");

# connect to the database
my $db = 'bakery_users';
my $host = 'localhost';
my $user = 'root';
my $pass = 'P@ssw0rd';
my $dbh   = DBI->connect ("DBI:mysql:database=$db:host=$host", $user, $pass) or die "Can't connect to database: $DBI::errstr\n";
# check the username and password in the database
my $statement = qq{SELECT id FROM users WHERE username=? and password=?};
my $sth = $dbh->prepare($statement)
  or die $dbh->errstr;
$sth->execute($username, $password)
  or die $sth->errstr;
my ($userID) = $sth->fetchrow_array;

# create a JSON string according to the database result
my $json = ($userID) ? 
  qq{{"success" : "login is successful", "userid" : "$userID"}} : 
  qq{{"error" : "username or password is wrong"}};

# return JSON string
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;
这是垃圾门户cgi页面,需要了解用户,以便提供一些特定信息

#!C:\Strawberry\perl\bin\perl.exe
use CGI;
use strict;
use warnings;

my $cgi = CGI->new; #new CGI routine

print $cgi->header('text/html'); #create HTTP header

print "<html> <head>\n";
print "<title>Hello, world!</title>";
print "</head>\n";
print "<body>\n";
print "<h1>Hello, world!</h1>\n";
print "</body> </html>\n";
#!C:\草莓\perl\bin\perl.exe
使用CGI;
严格使用;
使用警告;
my$cgi=cgi->新建#新的CGI例程
打印$cgi->标题('text/html')#创建HTTP头
打印“\n”;
打印“你好,世界!”;
打印“\n”;
打印“\n”;
打印“你好,世界!\n”;
打印“\n”;

大多数登录文件都是从IBM开发者页面上修改的,我可以通过另一个Perl模块CGI:Session传递CGI参数

加入

my $session = new CGI::Session(undef, undef) or die CGI::Session->errstr;
$session->param("username", $username);
my $cookie = $cgi->cookie(CGISESSID => $session->id);
print $cgi->header( -cookie=>$cookie );
到我的login.pl文件,并将以下块添加到我的portal.cgi

my $sid = $cgi->cookie('CGISESSID') || $cgi->param('CGISESSID') || undef; #loads previous cookie
my $session = load CGI::Session(undef, $sid);
my $user = $session->param("username");

这将在temp dir中创建一个本地cookie,并读取该cookie以查找我在login.pl页面上传入的“username”变量

我能够通过另一个Perl模块CGI:Session传递CGI参数

加入

my $session = new CGI::Session(undef, undef) or die CGI::Session->errstr;
$session->param("username", $username);
my $cookie = $cgi->cookie(CGISESSID => $session->id);
print $cgi->header( -cookie=>$cookie );
到我的login.pl文件,并将以下块添加到我的portal.cgi

my $sid = $cgi->cookie('CGISESSID') || $cgi->param('CGISESSID') || undef; #loads previous cookie
my $session = load CGI::Session(undef, $sid);
my $user = $session->param("username");

这将在temp dir中创建一个本地cookie,并进行读取,以查找我在login.pl页面上传入的“username”变量

任何人都不必费力地通过150(或其他)LOC来尝试帮助您。你能为我们把问题归结为一个问题吗?我在介绍中描述了我的问题是什么。我的代码是用来理解到目前为止我设置了什么。我想将用户转到另一个页面。如果您尝试,当然可以更简洁地再现问题。一个代码转储和一个为您调试代码并找出错误的请求是懒惰的。@Matt Jacob,每个文件都非常简单,并且没有什么可以从其中删除的。使用它们应该没有问题。当前的代码是有效的,我只需要一种方法将用户ID传递到它们打开的新页面。任何人都不必费力地通过150个(或其他)LOC来尝试帮助您。你能为我们把问题归结为一个问题吗?我在介绍中描述了我的问题是什么。我的代码是用来理解到目前为止我设置了什么。我想将用户转到另一个页面。如果您尝试,当然可以更简洁地再现问题。一个代码转储和一个为您调试代码并找出错误的请求是懒惰的。@Matt Jacob,每个文件都非常简单,并且没有什么可以从其中删除的。使用它们应该没有问题。当前的代码可以工作,我只需要一种方法将用户ID传递给它们打开的新页面