Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Run a perl script and return results using AJAX without reloading the page

by tangent (Parson)
on Mar 01, 2016 at 19:31 UTC ( [id://1156570]=note: print w/replies, xml ) Need Help??


in reply to Run a perl script and return results using AJAX without reloading the page

This may not solve your problem but as you are returning JSON you need to set that in the header, so instead of:
print "Content-type: text/html\n\n";
Try:
print "Content-type: application/json\n\n";
Also, you should really get the value of the query string by creating a new CGI object, so your script would be something like this:
#!/usr/bin/perl use strict; use warnings; use JSON; use CGI; my $cgi = CGI->new(); my $string = $cgi->param('s'); my $json = encode_json( [$string] ); print $cgi->header( -type => 'application/json' ); print $json;
Update:
You probably need to modify your Javascript function too:
function perlExecute(term){ $.ajax({ type: 'POST', url: '/cgi-bin/doma.pl', data: { 's': term }, success: function(res) { alert(res); }, error: function() {alert("did not work");} }); };

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1156570]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-23 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found