每次单击按钮时执行 CGI,而不更改当前 html 页面
我正在开始在家庭服务器上实习,该服务器能够从网页控制多个家庭自动化设备。总体想法是,基于单击按钮,在服务器上生成特定脚本并控制微控制器。
我的导师给我建立了一个简单的网站,使用 AJAX 始终停留在一页上,并根据用户操作显示菜单(如果不使用它们则隐藏,如果使用则返回到前面)。
我已经设置了一个 apache 服务器,将其配置为执行 CGI 脚本,并且它可以工作。 为了始终停留在一个页面上,我使用了“204 No Content”返回技巧,以便服务器对该页面的回答是“我没有什么可说的,只需停留在这个页面上”。
但我遇到的一个问题是 CGI 只启动一次。如果我第一次单击该按钮,它会起作用,之后什么也不会发生。
我尝试使用 SSI (shtml) 来使用按钮代码,而不是使用带有 GET 方法的 FORM,但脚本仍然不会执行两次。
我可能使用了错误的工具。我应该继续使用 CGI 吗?或者还有其他东西(比如 AJAX、jquery)实际上是为了做我想做的事情而设计的吗?
感谢您的阅读。
编辑:我找到了解决方法(总是当我在寻找答案几天后绝望时,我会去论坛,然后在接下来的一个小时内找到一个不错的解决方案......) 我使用了一个基本链接,由于某种原因,它的行为与使用按钮不同。任何。我对所使用的技术的质疑仍然存在:)
EDIT2 :我的解决方案很糟糕,由于某种原因,该脚本也在页面刷新时(或第一次加载页面时)被调用。这很奇怪,因为因为它位于它中,所以只有当我单击它时才会产生它......
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 ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
熟悉 jQuery 及其 AJAX API。除非使用 AJAX,否则无法使其不加载新页面。以下是 AJAX 调用的示例:
适用于 jQuery 1.5 或更高版本。每当单击某个按钮时,您都可以运行它,如下所示:
按钮的 HTML:
JavaScript:
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:
That is for jQuery 1.5 or higher. You can run that whenever a certain button is clicked like this:
HTML for the button:
JavaScript: