views:

72

answers:

3

I have an embedded application running httpd for a web server. On the main page I have two buttons. One uses JQuery to add a click event with the following code:

$.get("example.cgi");

The other button is the submit button for a blank form (i.e. other than the form tags and the input with type = "submit" the form contains no fields). The method for the form is set to "get" and the action set to "example.cgi". When I press button one, the script fails. When I press button two, the embedded device responds accordingly.

I would really like to use JQuery for this entire project (for example, specifically so I dont HAVE to have blank forms all over my pages...), but I cannot figure out what the difference would be between a blank form with a method "get" and a JQuery $.get();. Does anyone know what might be happening?

UPDATE:

the html and js code in its entirety --

<!doctype html>
<html>
<head>
<title>LCRFLC on the WEB</title>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
    // add click events
    $("#off").click(function(event)
    {
        $.get("rflcc.cgi", { cmd: "RAMP", lvl: "0" } );
    });
    $("#fon").click(function(event)
    {
        $.get("rflcc.cgi"); 
    });
});
</script>
</head>
<body>
<h1>TEST PAGE FOR RFLC CONTROL AND STATUS</h1>
<input type="button" value="Off" id="off">
<input type="button" value="Full On" id="fon">
<form method="get" action="rflcc.cgi" id="alt" name="alt">
alternate method
<input type="submit" value="Another try" name="altsubmit">
</form>
<br>
<textarea cols="80" rows="50"></textarea>
</body>
</html>

this may require a little more explanation now, so i will take a short attempt at that. the only reason the two click events are different is so i could try two different things with one load (bc i have to flash the embedded device with this static code every time...). neither of those get methods in the click event work though. the blank form however DOES work, and my original question was what is the difference between that blank form and the jquery get in the click event for the 'fon' button? i was thinking they should behave the same...

A: 

Which is failing - the JQuery get or your blank form?

Dav Evans
the JQuery get is failing.
Toddeman
This should have been a comment not an answer, but it is a valid question.
James Black
+3  A: 

Are you sure you are using $.get correctly. It fetches the page asynchronously and executes the callback function you specify. On the other hand, when the empty form is submitted the page is changed.

This would work for you.

HTML

<button id="get">Get</button>
<div id="target"></div>

Javascript

$('#get').click(function() {
    $.get('example.cgi', function(data) {
        $('#target').html(data);
    });
});
Nithesh Chandra
i have read the jquery get documentation, and it says i may use it calling only $.get('example.cgi');. i can try the above code though just to make sure.
Toddeman
Its legal to call $.get('example.cgi') but the returned data gets lost. I'm not sure why it is even allowed but that's how it works. If you want to do something with the returned data, you HAVE to define a callback function. There's a simpler alternative if you are interested. Check for "load" function in JQuery docs.
Nithesh Chandra
maybe we have reached the source of my problem. at this point, i dont WANT to do anything with the return data. those buttons above turn on and off lights. all i care is that the 'rflcc.cgi' gets called. once it does, whatever it returns may be discarded. should i be using jquery load instead? (thank you for your patience, i am new to jquery)
Toddeman
It sounds like $.get with no callback is actually what you want.
RickF
I agree with RickF. If you just need to execute some server-side script when the button is clicked, just use $.get to call it. Though, I would suggest using $.post which makes it more clear about your intent. A GET request is not supposed to affect the server-side information. You should use POST for that.
Nithesh Chandra
errr... this is getting frustrating now. im beginning to think that its something wrong with the implementation of the server on the embedded device. i tried all of the above. a jquery get fails. a jquery post fails. a blank form post fails. a blank form get succeeds. the only thing that succeeds is a blank form get. im am now thoroughly lost... (especially since, as you guys mentioned above, i SHOULD be using a POST... that makes most sense)
Toddeman
+1  A: 

As @Nithesh commented, $.post() is really the right tool for the job when you're not getting information back. As a bonus, POST requests should never be cached by the browser, but GET requests can be, and that theoretically could be your problem.

Also, your blank form isn't truly blank - the submit button has a name and value which get submitted as parameters. Is it possible that your cgi script is where the problem lies instead? It seems unlikely, but I have to ask...

For diagnostic purposes, you might want to do something like this:

$("#fon").click(function(event) {
    $.post("rflcc.cgi", function() { alert "Done!"; });  
});

That will tell you if the request is happening at all. If you're not already, look into the FireBug addon for Firefox as a tool to help diagnose Ajax stuff.

RickF
i have firebug, im not really sure what to look at though. for problems like this before i have always checked the 'net' tab. i could see when requests were going out and when they werent, but thats about the extent of my knowledge on that. im going to try what you just posted though, i want to see if even THAT works.
Toddeman
found the problem... completely unrelated. i marked your answer because it led me to find the real problem. thanks
Toddeman