dannyhmg has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use AJAX to access a Perl app that uses CGI::Application. I am trying to select the Runmode by passing the  rm parameter with the AJAX call. Here's the relevant part of my JS code:
$.ajax({ type: "POST", url: "someurl.pl", rm: "my_runmode", dataType: "json", error: function (jqXHR1, textStatus1, errorThrown1) { // Do something }, success: function( response ) { // Do something else } } });
However, this doesn't select the correct Runmode; it always uses the StartRunmode, irrespective of the value of the rm parameter. I've also tried omitting the rm parameter and setting the url to  someurl.pl?rm=my_runmode, but no dice. What am I doing wrong here?

Replies are listed 'Best First'.
Re: Selecting CGI::Application with jQuery AJAX
by Corion (Patriarch) on Apr 14, 2015 at 14:36 UTC

    Are you sure that $.ajax() accepts and passes on random parameters?

    Have you verified that what you want to send actually gets sent from jQuery? If not, this is more a jQuery problem than a Perl problem.

    I would assume that CGI::Application takes the parameters from the POST data and that jQuery.ajax sends the data from the data key and not from random parameters stuffed somewhere.

Re: Selecting CGI::Application with jQuery AJAX
by afoken (Chancellor) on Apr 14, 2015 at 18:13 UTC
    ...

    Read http://api.jquery.com/jQuery.ajax/: The ajax method expects the data in the data key of the settings object, not in the settings object. The data may be an object, an array or a string.

    Example, stolen from the same page:

    $.ajax({ method: "POST", url: "some.php", data: { name: "John", location: "Boston" } // <-- look here! }).done(function( msg ) { alert( "Data Saved: " + msg ); })

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Selecting CGI::Application with jQuery AJAX
by jeffa (Bishop) on Apr 14, 2015 at 16:54 UTC

    There is no such jQuery setting called rm, is there? That is just a query parameter, so treat it is as such, as you tried in your second attempt. The reason why i believe someurl.pl?rm=my_runmode is not working for you is because your Ajax call uses POST, not GET. Failure tends to happen when you mix your HTTP methods. Try passing rm via POST.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Selecting CGI::Application with jQuery AJAX
by graff (Chancellor) on Apr 14, 2015 at 18:01 UTC
    Maybe my experience with (or my use of) CGI::Application is different from yours, but for me, a typical url for executing "run_mode_x" via CGI::Application starts like this:
    "someurl.pl/run_mode_x$params"
    where $params is either an empty string or else something like:
    "?param1=value1(&...)"
    depending on whether or not the request needs to pass any parameter(s) to the chosen run_mode.
Re: Selecting CGI::Application with jQuery AJAX
by Anonymous Monk on Apr 14, 2015 at 22:57 UTC