How do I get my Perl script to execute from JavaScript called from an HTML form's 'action' attribute?
I am using scripts built by other people mostly, and the basic gist of it is that I need to call the CGI script from the JS function.
I have to do it this way because it was the best I could come up with based on whether or not the device is mobile.
I need to pass some checkboxes to the Perl script and return some files based on the checkbox states. I know the Perl script works perfectly without having to execute the JavaScript, but I don't know how to check if a device is mobile or not in Perl, and I do in JS.
All I need is to execute the CGI script, passing it the same information as if it was being called directly from the form, but it does nothing.
Here is my JavaScript:
function downloadFiles(){
var isMobile = function(){
try {document.createEvent("TouchEvent"); return true;}
catch(e) {return false;}
}
&& (/Mobi/i.test(navigator.userAgent));
if(!isMobile){
$.ajax({
type:"Post",
url:"https://www.mywebsite.com/cgi-bin/myPerlScript.cgi",
success:function(msg){$("#myHtmlForm").html(msg.d);}
});
}
}
I don't rightly know what 'msg' or its property 'd' is, and I don't even know if my form should be the element where it is. So what I am missing here?
It turns out that I wasn't building the ajax statement completely or correctly. I didn't add any data that I needed to pass into the CGI script.
Here's the ajax statement I ended up with:
$.ajax({
type:"POST",
url:"/cgi-bin/myPerlScript.cgi",
data:$("#myHtmlForm").serialize(),
error:function badCall(){location.href="https://www.mywebsite.com/error.html";}
});
Thank you @ikegami, @mkHun, @simbabque, and @DaveCross!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.