如何使用cgi返回到同一个html页面?

发布于 2024-08-14 05:40:05 字数 856 浏览 2 评论 0原文

我有 C 函数。从html页面,通过ajaxcall传递请求并得到响应。

如果我执行某些操作(例如单击提交按钮),则会执行某些操作。重定向后如何回到同一个html页面?

我可以设置自动重定向到该页面的环境变量吗?


编辑:从下面的“答案”中添加更多详细信息

谢谢罗伊。

由于我是新手,所以我无法得到它。如果我们使用 php,那么我可以获取 URL 并将其放入标头中(“Location:”.$url)。

但我只有 C 函数。

HTML代码


function x(id)
{
    var val;
    var URL = window.location.href;
    if(id =="y")
    {
        val=1;
        document.location = "http://ipaddr/cgi-bin/file.cgi?value="+val+"&url="+URL;
    }
}

C代码


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    printf("Content-type: text/html; charset=utf-8\n\n");

    char* data = getenv("QUERY_STRING");
        //process
}

如何从C返回到同一页面?如何给出 header("Location:".$url);在C语言中??

I am having C function. From the html page, through ajaxcall the request is passed and the response is got.

If I perform some action like click submit button, then some action will be performed. How to get back to the same html page after redirection?

Can I set the environment variable which automatically redirects me to that page?


EDIT: Adding more detail from 'answer' below

Thank you Roe.

As I am new to this I am not able to get it. If we are using php then I can get the URL and put it in the header("Location:".$url).

But I am having only C functions.

HTML Code


function x(id)
{
    var val;
    var URL = window.location.href;
    if(id =="y")
    {
        val=1;
        document.location = "http://ipaddr/cgi-bin/file.cgi?value="+val+"&url="+URL;
    }
}

C Code


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    printf("Content-type: text/html; charset=utf-8\n\n");

    char* data = getenv("QUERY_STRING");
        //process
}

How to return from C to the same page? How to give header("Location:".$url); in C??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贵在坚持 2024-08-21 05:40:05

严格来说,这与cgi和c都没有关系,但无论如何。

您有几个选择,我想最好的是将当前页面作为 < /code>,这样您就可以在表单提交中将其作为变量获取,并且可以通过在响应中添加 Location: 标头(或通过其他方式)从脚本重定向回该页面,例如确认页面上的链接和 http-refresh 元标记,或者您正在执行的任何操作)。

编辑
有提到的引荐来源网址,但这可能会也可能不会设置(我想浏览器有“隐私”插件可以删除引荐来源网址标头)。

现在有 http-equiv 刷新选项,该选项在另一个答案中有详细介绍,因此我将向您展示 Location-header 样式。这实际上是cgi。 :) CGI 协议规定您应该以标头开始输出(假设您没有执行 NPH-CGI,但如果您执行的是,我希望您知道自己在做什么),后跟一个空行。只需将其作为标头:

printf ("Status: 302 Moved Temporarily\r\n");
printf ("Location: %s\r\n", url);
printf ("\r\n"); /* end of header */

您需要解析查询字符串(如果是 GET 请求,则解析 stdin,但这有点复杂)并提取 url。您可以进行任何您喜欢的处理,但请注意,您输出的任何数据很可能永远不会显示给用户(与 http-equiv 刷新相反,它可能会导致浏览器刷新内容,无论是否为空) ,在进入下一页之前简要介绍)。

301(永久移动)会被缓存,浏览器下次可能会绕过你的cgi脚本并直接访问该url,这不是你想要的。

Strictly speaking, this has nothing to do with neither cgi nor c, but anyway.

You have a couple of options, I suppose the best would be to put the current page as an <input type="hidden" name="currentPage" value="/my/current/page"/>, that way you'll get it in your form submission as a variable and you can redirect back to that page from your script by putting a Location: header in your response (or by other means, such as a link and a http-refresh meta tag on your confirmation page, or whatever you're doing).

EDIT
There's the referrer as mentioned, but this might or might not be set (I suppose there are 'privacy' plugins for browsers which remove the referrer header).

Now there's the http-equiv refresh option, which is detailed in another answer, so I'll show you the Location-header style. And this is actually cgi. :) The CGI protocol says you should start your output with a header (assuming you're not doing NPH-CGI, but if you are, I hope you know what you're doing) followed by an empty line. Simply put this as your header:

printf ("Status: 302 Moved Temporarily\r\n");
printf ("Location: %s\r\n", url);
printf ("\r\n"); /* end of header */

You need to parse the query string (in case of a GET request, otherwise parse stdin, but that's a little bit more complicated) and extract the url. You can do any kind of processing you like, although be aware that any data you output will most likely never be displayed to the user (as opposed to the http-equiv refresh is likely to cause the browser to flash the content, empty or not, briefly before moving to the next page).

301s (moved permanently) are cached, and the browser might bypass your cgi script the next time and go to the url directly, which is not what you want.

吃颗糖壮壮胆 2024-08-21 05:40:05

引用页面由环境变量HTTP_REFERER指定。您可以使用重定向页面返回到原来的页面,如下所示:

const char * redirect_page_format =
"<html>\n"
"<head>\n"
"<meta http-equiv=\"REFRESH\"\n"
"content=\"0;url=%s\">\n"
"</head>\n"
"</html>\n";
printf (redirect_page_format, getenv (HTTP_REFERER));

理想情况下,您应该在执行此操作之前检查 HTTP_REFERER 是否是有效的环境变量。

The referring page is given by the environment variable HTTP_REFERER. You can get back to the same page you came from by using a redirect page, like the following:

const char * redirect_page_format =
"<html>\n"
"<head>\n"
"<meta http-equiv=\"REFRESH\"\n"
"content=\"0;url=%s\">\n"
"</head>\n"
"</html>\n";
printf (redirect_page_format, getenv (HTTP_REFERER));

Ideally you should check that HTTP_REFERER is a valid environment variable before doing this.

琴流音 2024-08-21 05:40:05

你的问题相当不清楚,但如果我理解正确的话:向用户展示一些看起来像提交按钮的东西,但实际上只是执行你的 AJAX 操作。您停留在同一个 HTML 页面上,从服务器获取返回结果,并以任何适当的方式更改页面。

Your question is rather unclear, but if I understand you correctly: present the user with something that looks like a submit button, but actually just performs your AJAX operation. You stay on the same HTML page, get back your return from the server, and alter the page in whatever manner is appropriate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文