Hello! I am very new to Perl and looking on how to use a JQuery AJAX call to execute my perl script. I would like the perl script to return a JSON object for my browser as well (in this case pulling from SQLite Database). I am receiving an error response on the AJAX catch. In response obj (responseText) I can see the text content of the perl file. So my first problem is why the perl script not executing? Every example I can find looks as similar as mine. Any pointers would be very appreciated!

function ajaxPerl() { $.ajax({ method: 'GET', url: `../cgi-bin/test-dbi.pl`, dataType: 'json', header: { "content-type": "application/json;charset=utf-8" } }).then((response) => { console.log('RESPONSE -> ', response); }).catch((error) => { console.log('ERROR -> ', error); }); }

My Perl is below - I am not sure it is returning a JSON file to the browser correctly at this point - but it does format it in the terminal with out errors.

#!/usr/bin/perl use strict; use warnings; use CGI; use DBI; use JSON; my $driver = "SQLite"; my $database = "../data/test.db"; my $dsn = "DBI:$driver:dbname=$database"; my $userid = ""; my $password = ""; my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) or die $DBI::errstr; print "Opened database successfully\n"; # CREATE TABLE my $stmt = qq(CREATE TABLE MYTABLE (ID INT PRIMARY KEY NOT NULL, DATE TEXT NOT NULL, TIME TEXT NOT NULL, DESCRIPTION CHAR(250));); my $rv = $dbh->do($stmt); if($rv < 0) { print $DBI::errstr; } else { print "Table created successfully\n"; } $stmt = qq(INSERT INTO MYTABLE (ID, DATE, TIME, DESCRIPTION) VALUES (1, '2017-05-13', '07:55:00', 'The very first chicken') +;); $rv = $dbh->do($stmt) or die $DBI::errstr; print "Records created successfully\n"; # convert to JSON my @output; my $sth = $dbh->prepare('select DATE, TIME, DESCRIPTION from MYTABLE +'); $sth->execute; while ( my $row = $sth->fetchrow_hashref ){ push @output, $row; } my $cgi = CGI->new; print $cgi->header( 'application/json' ); print to_json( { myData => \@output } ); $dbh->disconnect();

In reply to AJAX call to Perl script + return - error by Noben

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.