Execute a CGI each time a button was clicked without changing the current html page

倖福魔咒の 提交于 2021-02-10 19:56:42

问题


I am starting my internship on a Home Server able to control mutliple domotics equipments from a web page. The global idea is that based on a click on a button, a certain script is spawned on the server and controls a microcontroller.

My tutor built a simple website he gave me, using AJAX to always stay on 1 page, and brings the menus according to user actions (they are hidden if not used, brought back to front if used).

I have set up an apache server which I configured to execute CGI scripts, and it works. To always stay on one page, I used the '204 No Content' return trick, so that the server's answer to the page is 'I don't have anything to say, just stay on this page'.

But the one problem I have is that the CGI is launched only once. If I click the button for the first time it works, afterwards nothing happens.

I tried using the SSI (shtml) to use the in a button code instead of using a FORM with GET method but the script still won't execute twice.

I might be using the wrong tools. Should I keep going with CGIs ? Or is there something else (like AJAX, jquery) that actually is designed to do what I want ?

Thanks for having read.

EDIT : I have found a way around it (it's always when I'm desperate after looking for days for an answer that I go to forums and then find myself a nice solution in the next hour .... ) I used a basic link, and for some reason it has a different behaviour than using a button. Whatever. My interrogations on the technologies used still stand though :)

EDIT2 : My solution is crappy, for some reason the script is also called at page refresh (or when the page loads for the first time). It's strange, because since it's in the it should only be spawned when I click on it ...


回答1:


Familiarize yourself with jQuery and its AJAX API. You can't make it not load a new page unless you use AJAX. Here is an example of what an AJAX call looks like:

$.ajax({
    url: 'http://server.domain.com/cgi-bin/myfile.cgi',
    data: {
       x: 1,
       today: '20110504',
       user: 'Joe'
    }
}).success(function(data, status, xhr) {
    if (data)
        alert(data);
});

That is for jQuery 1.5 or higher. You can run that whenever a certain button is clicked like this:

HTML for the button:

<input type="button" id="doThis"/>

JavaScript:

$(function() {
    $('#doThis').click(function() {
        //put AJAX sample shown above, here
    });
});


来源:https://stackoverflow.com/questions/5882569/execute-a-cgi-each-time-a-button-was-clicked-without-changing-the-current-html-p

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!