Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

retrieving value from ajax call to cgi page

by gayu_justin (Novice)
on Jun 29, 2013 at 09:54 UTC ( #1041440=perlquestion: print w/ replies, xml ) Need Help??
gayu_justin has asked for the wisdom of the Perl Monks concerning the following question:

i have made an ajax call and passed some values to a cgi page.following is my ajax code

var value1 = jQuery(this).find("INPUT").val(); $.ajax({ url: 'process.cgi', // The type of request. type: "get", data : {'value1' ; value1}, success: function( data ){ alert(data); } });

And i want to take the value in the process.cgi file.i just used the code like below :

print $cgi->param('value1');

BUt it is not printing there.what is the correct method to retrieve the value from the ajax call.should i use any ajax directives in the cgi page.please help me doing this?i checked so many sites and i am stuck

Comment on retrieving value from ajax call to cgi page
Select or Download Code
Re: retrieving value from ajax call to cgi page
by thomas895 (Hermit) on Jun 29, 2013 at 10:05 UTC

    "It is not printing". Mmm-hmm, okay, very well.
    As in, you get an empty response? Or an error message of some kind?

    Did you verify that your JavaScript actually works? I don't know much about (what looks like jQuery) that, but you should use a tool like Firebug or your browser's console to check that the correct request is being sent.

    ~Thomas~ 
    "Excuse me for butting in, but I'm interrupt-driven..."
      yeah, i have checked through the firebug and found that the values are passing and also i created a test page and check whether the ajax call is alerting the content of the page and found that, it is working.I think, the problem is with the method, that i use to take the value.

        Okay, let's assume that your javascript does indeed work, and that the problem lies with your script.
        In that case, we will need a more complete example of your code. Is the $cgi object in the current scope? Did you turn on strict and warnings?

        For now, try the following sample:

        #!/usr/bin/perl use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); # not needed per se, but very usefu +l for debugging use CGI; my $cgi = new CGI(); print $cgi->header("text/javascript"); print "alert('", $cgi->param("value1") || "nothing", "');\n";
        ~Thomas~ 
        "Excuse me for butting in, but I'm interrupt-driven..."
Re: retrieving value from ajax call to cgi page
by moritz (Cardinal) on Jun 29, 2013 at 11:22 UTC
                      data : {'value1' ; value1},

    This looks wrong. The correct way to write it would be {value1: value1}.

    I highly recommend using firebug or chrome's developer tools (the one that opens when you press F12) when doing anything with javascript; otherwise the danger of running into javascript errors that are silently ignored is quite high.

      On checking, i found that the page process.cgi is getting the value and since, this is an jax call, the value will not get print in the browser. please help me to take the value and all the text box value will be in the single variable

        Help me understand 'will not print in the browser.' Your JQuery snippet above just alerts the data, are you seeing the value in the alert box or not?

        If not, when you click on the network tab in firebug, look for your URL and ensure the response code was 200 (or whatever success code you're sending). If the ajax call http response was not successful, check your server error logs for any server side issues.

        If the ajax call was successful, click on the URL in firebug and click on the response tab. Is the data coming back the way you expect? If not, then check your server side logic to ensure you're doing what needs to be done (you can post your perl snippets then).

        If the data is coming back correctly, then double check the console tab in firebug -- you may have some other javascript error that's causing problems.

        -derby

        update: forgot to add, there's nothing special about AJAX for the perl/server side perspective. Wether the call is AJAX or the result of a click on an anchor element, perl/server side could care less. Many times when I'm debugging JQuery, I do the testing outside of the page on which the AJAX call resides. Wether that's a simple http://foo.com/cgi-bin/process.cgi?value=bar call on the browser or something more complicated via POSTing, it doesn't matter.

        try data : { value1: 'value1' },

        Here is my working test, an html page

        <html> <head> <script src="jquery-2.0.2.min.js"></script> <title>test-ajax.htm</title> <script> $.ajax({ url: 'process.cgi', // The type of request. type: "get", data : { value1: 'value1' }, success: function( data ){ alert(data); $( "#value1" ).html( "<strong>" + data + "</strong>" ); } }); </script> </head> <body> <span style="background-color:yellow" id="value1">?</span> </body> </html>
        and a process.cgi script.
        #!perl # process.cgi use strict; use CGI; my $q = CGI->new; print $q->header; print $q->start_html, 'Data returned is '.$q->param('value1'), $q->end_html;
        poj

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1041440]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-10-26 07:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (152 votes), past polls