0

I'm kinda new to all this, so tell me if I'm doing something totally wrong.

I'm doing some gpio stuff with my raspberry pi, and at the moment i'm making something so the gpio pins can be controlled via a web interface. One of the ways I'm doing this is using bash CGI scripts to control the pins, and executing them from the browser.

So far the only way I can get this to work involves the browser loading the page ".../cgi-bin/gpio1.cgi" etc, which contains the code:

#!/bin/bash
echo "Content-type: text/html"
echo ""
...gpio stuff...

This works, but the browser navigates to the blank page created by this script.

Is there a way of executing these scripts without leaving the webpage, and so the scripts aren't writing HTML but instead focusing on the actual gpio stuff?

Thanks

4

1 回答 1

1

尝试这个:

#!/bin/bash
echo "Status: 204 No Content"
...gpio stuff...

HTTP 响应必须以状态行开头;如果 CGI 没有指定状态,网络服务器通常会添加状态“200 Ok”。该状态必须伴随响应正文,这将形成新的网页。

您想要的状态是204,表示请求已满足,但没有响应,浏览器应保持在同一页面上。通常,这将是对请求的响应POST,而不是GET请求,但无论如何它应该可以工作。由于204响应不需要响应正文(实际上,它不允许),因此不必在状态行之后输出空行,但如果脚本需要很长时间才能运行,您可能需要一个.

于 2013-06-13T04:16:27.197 回答