0

Wow, this is convoluted, but I basically need to trigger the $.ajax function in a web page, via a separate Perl script.

I Have a web page whose $.ajax calls a Perl file which grabs data and returns it to the web page. Then javascript analyzes the data and creates another javascript function call which asks Perl to send an email with that data. [Perl grabs data, sends to js, js calls Perl again to email the analyzed data].

I am asked to write a Perl script that will do the emailing separately, but I desperately need to use existing code; so, can my Perl script call that particular $.ajax to get the data, analyze it then send it again to Perl for emailing?

Any ideas??

dcparham
  • 291
  • 4
  • 17
  • You'd be better off refactoring your code so that the code that the Ajax request is hitting becomes a reusable module for actually getting the data, and a thin wrapper for providing it over Ajax. Then your email sending module can use the data getting module and ignore the Ajax wrapper. – Quentin Nov 18 '14 at 16:28

1 Answers1

0

You can have the ajax call a different Perl/CGI file as long as they are on the same domain (possible but more complicated).

Just specify the different url in the $.ajax call.

$('#cgi1').on('click', function(){
    $.ajax({
       url: "perl-01.cgi"
    }).done(function() {
       alert( "cgi 1 done" );
    });
});

$('#cgi2').on('click', function(){
    $.ajax({
       url: "perl-02.cgi"
    }).done(function() {
       alert( "cgi 2 done" );
    });
});
Zach Leighton
  • 1,939
  • 14
  • 24
  • thx for being there, but let me clarify: i need to run a .pl script via a cron on the server that calls the $.ajax which calls cgi1.pl. the web page already does the $.ajax call to cgi1.pl, and i can put a timer on it to run at intervals. but we want to: initiate the run of just that one function, via a cron job[.pl script]. pls let me know if i am not clear. thx!! – dcparham Nov 18 '14 at 15:26
  • You lost me, why can't you just make one ajax call get the data back and then call another? Also perl can use LWP::UserAgent to make a http request to another pl script. – Zach Leighton Nov 18 '14 at 15:28
  • i know it's convoluted[!] - i already make 1 ajax call to perl to get data back, it returns, i analyze it then if there are concerns, i make another ajax call where i send warning info to perl to email it. i want to make that original .ajax call via perl, outside the scope of the webpage. will also look at your LWP::UserAgent. i don't mean to be frustrating the person trying to help, thx again! – dcparham Nov 18 '14 at 15:59
  • 1
    Use LWP::UserAgent for a quick hack but you may want to refactor your process here – Zach Leighton Nov 18 '14 at 16:49
  • thx, Zach u are spot on: here is the process now: use $.ajax call to perl for db data which is returned to .js file, analyzed and displayed as warnings where applicable. *then* a separate .pl file to get data from db and analyze it [via perl]; then where applicable, send emails to managers. this way: the browser app shows display of warnings; perl file handles emails, createTicket() function/whatever. similar, but the js is for display, the perl handles notifications. i really appreciate your input!! [selected 1^ as ur comments add helpful direction!] – dcparham Nov 19 '14 at 02:12