如何从POST请求附加到HTML表单URL [英] How to append to an HTML form URL from a POST request

查看:63
本文介绍了如何从POST请求附加到HTML表单URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想采用HTML表单的字段之一,并使用它来修改操作的URL.我遵循了这个问题,它显示了如何处理GET请求,但是我我在修改POST请求的技术时遇到了麻烦.

I want to take one of the fields of an HTML form and use it to modify the URL for the action. I followed this question, which shows how it's done for a GET request, but I'm having trouble amending the technique for a POST request.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>User location lookup</title>
    </head>

    <body>
        <!-- method: https://stackoverflow.com/questions/11811782/form-value-creates-a-url -->

        <script>
            function process()
            {
                var url="http://localhost:43/" + document.getElementById("url").value;
                location.href=url;
                return false;
            }
        </script>

        <form onSubmit="return process();" method="get">
            Username: <input type="text" name="url" id="url"></input>
            <input type="submit" value="go"></input>
        </form>

        <script>
            function process2()
            {
                var url="http://localhost:43/" + document.getElementById("url").value;
                location.href=url;
                return false;
            }
        </script>

        <form method="post"  onSubmit="return process2();">
            Username: <input type="text" name="url" id="url"> </input>
            Location: <input type="text" name="location" id="location"> </input>
            <input type="submit" value="go"></input>
        </form>
    </body>
</html>

GET请求正确发送:

The GET request correctly sends:

GET /brian HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-GB
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: localhost:43
DNT: 1
Connection: Keep-Alive

关于POST的信息,我得到了:

Whereas for the POST I get:

GET /brian HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-GB
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: localhost:43
DNT: 1
Connection: Keep-Alive

哪一个仍然是GET,即使该方法是 POST .

Which is still a GET, even though the method is POST.

我想生成的是:

POST /brian HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-GB
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: localhost:43
Content-Length: 11
DNT: 1
Connection: Keep-Alive

location=SO

有人可以用尽可能少的JavaScript代码指向我正确的方向吗?

Can someone point me in the right direction, with as little JavaScript code as possible?

推荐答案

您的代码未提交表单.它实际上是通过 GET 直接调用URL的,即:该方法仅适用于GET.

Your code isn't submitting the form. It's actually directly calling the URL via GET, i.e.: This method only works with GET.

请尝试:

<form id="myformid" method="post">
    Username: <input type="text" name="url" id="url2" onChange="updateAction()"> </input>
    Location: <input type="text" name="location" id="location"> </input>
    <input id="mysubmit" type="submit" value="go"></input>
</form>

<script>
    function updateAction() {
        var url = document.getElementById("url2").value;
        document.getElementById("myformid").setAttribute("action", "http://localhost:43/" + url);
    }
</script>

这篇关于如何从POST请求附加到HTML表单URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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