How to send data to a Perl script via ajax?

I want to send data to a Perl script through ajax and get the json format from it. But that will not work. I know that something is wrong in the following scenarios. Does anyone know how to fix this?

JQuery Code:

$("#test").click(function(){
    var ID = 100;
    var data = {
        data_id : ID                                                                        
    };

    $.ajax({        
        type: "POST",
        url: "ajax.cgi",
        data: data,
        success: function(msg){
            window.alert(msg);
        }       
    });
});

ajax.cgi (perl script):

#!/usr/bin/perl

use CGI;
use DBI;

$cgi = CGI->new;

# Here I'd like to receive data from jQuery via ajax.
$id = $cgi->param('data_id');     
$json = qq{{"ID" : "$id"}};

$cgi->header(-type => "application/json", -charset => "utf-8");
print $json;

exit;
+4
source share
3 answers

Not sure if you have solved it now, but maybe someone else stumbles over this question and wonders how it works.

, . , index.html html (,/var/www/html) perl script cgi-bin (,/var/www/cgi-bin). perl script! cgi /cgi -bin/ajax/stackCGI - , .

, Perl cgi, AJAX JSON: click, , Javascript Perl AJAX JSON: .

index.html

<!DOCTYPE html>
<html>
    <head>
        <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>
    </head>
    <body>

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

    </body>
</html>

ajax.pl

#!/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;
+8

, :

$cgi->header(-type => "application/json", -charset => "utf-8");

print $cgi->header(-type => "application/json", -charset => "utf-8");
+1

This is actually not an answer, but simply a question related to one of the answers.

I am new to Perl and I just copied Cleb's answer (and also made the ajax.pl executable), but it does not work and throws this error:

XML Parsing Error: not well-formed
Location: file:///home/ali/workspace/snmp-agent/query_ui/ajax.pl
Line Number 1, Column 2:

As Cleb's answer shows, the first line is "#! / Usr / bin / perl". Can someone tell me, please, how to solve this problem?

-1
source