每次单击按钮时都执行CGI,而不更改当前的html页面 [英] Execute a CGI each time a button was clicked without changing the current html page

查看:81
本文介绍了每次单击按钮时都执行CGI,而不更改当前的html页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个家庭服务器上开始我的实习,该服务器能够从网页上控制多个家庭影院设备.全局的想法是,基于对按钮的单击,服务器上会生成特定的脚本并控制微控制器.

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.

我的导师建立了一个他给我的简单网站,使用AJAX始终停留在一页上,并根据用户操作显示菜单(如果不使用,则将其隐藏;如果使用,则返回至最前).

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).

我已经设置了一个配置为执行CGI脚本的apache服务器,它可以工作.为了始终停留在一页上,我使用了"204 No Content"返回技巧,以便服务器对该页面的回答是我无话可说,只停留在此页面上".

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'.

但是我遇到的一个问题是CGI只能启动一次.如果我是第一次单击该按钮,那么此后什么也没发生.

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.

我尝试使用SSI(shtml)在按钮代码中使用,而不是使用带有GET方法的FORM,但是脚本仍然不会执行两次.

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.

我可能使用了错误的工具.我应该继续使用CGI吗?还是还有其他东西(例如AJAX,jquery)实际上旨在满足我的需求?

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 ?

感谢阅读.

我已经找到了解决方法(通常是我在寻找答案的几天后绝望,我去了论坛,然后在接下来的一个小时内找到了一个不错的解决方案....)我使用了基本链接,由于某种原因,它的行为不同于使用按钮的行为.任何.我对所使用技术的质疑仍然存在:)

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 ...

推荐答案

熟悉jQuery及其 AJAXAPI .除非使用AJAX,否则不能使其不加载新页面.这是AJAX调用的示例:

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);
});

适用于jQuery 1.5或更高版本.只要单击某个按钮,就可以运行该命令,如下所示:

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

按钮的HTML:

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

JavaScript:

JavaScript:

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

这篇关于每次单击按钮时都执行CGI,而不更改当前的html页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆