如何在没有表单的POST请求中发送url参数

时间:2022-11-23 17:46:24

I have to pass a parameter in url. I can't send it in as usual GET request the variable and value is shown in the address bar with the request. So, the end user can change the value for this variable value and send request which will be processed.

我必须在url中传递一个参数。我不能像往常一样发送它GET请求变量和值显示在地址栏中的请求。因此,最终用户可以更改这个变量值的值并发送将要处理的请求。

href="url=/admin/usermanagement/?flag=2

I want to send this hiding flag=2

我想把这个隐藏标志设为2

now this goes as a GET request and it is seen in the address bar. Please give your suggestion if you have any idea on changing this to POST to hide and send the value.

这是一个GET请求,在地址栏中可以看到。如果您有任何想法,请将此更改为POST以隐藏和发送该值。

2 个解决方案

#1


4  

You can still use a html form but "hide" it from the user:

你仍然可以使用一个html表单,但是“隐藏”它从用户:

<!DOCTYPE html>
<html>
    <body>
        <form id="myform" method="post" action="/">
            {% csrf_token %}
            <input type="hidden" name="flag" value="2" />
            <a href="#" onclick="document.forms[0].submit();return false;">Let's go!</a>
        </form>
    </body>
</html>

And the view:

和观点:

def index(request):
    if request.method == 'POST':
        try:
            flag = request.POST['flag']
            # TODO use flag
        except KeyError:
            print 'Where is my flag?'

    return render_to_response('index.html', {},
        context_instance=RequestContext(request))

#2


3  

You can use AJAX to get rid of forms entirely.

您可以使用AJAX完全删除表单。

Just add this to your JavaScript:

只需将其添加到JavaScript中:

function postTo(url, query) {
    var request = (XMLHttpRequest?new XMLHttpRequest():new ActiveXObject());
    request.open('POST', url, true);
    request.send(query);
}

Then call with something like this:

然后像这样打电话:

postTo('/admin/usermanagement/','flag=2');

Note that this will NOT reload the page. If you want to reload the page, use Borges' answer.

注意,这不会重新加载页面。如果您想重新加载页面,请使用Borges的答案。

#1


4  

You can still use a html form but "hide" it from the user:

你仍然可以使用一个html表单,但是“隐藏”它从用户:

<!DOCTYPE html>
<html>
    <body>
        <form id="myform" method="post" action="/">
            {% csrf_token %}
            <input type="hidden" name="flag" value="2" />
            <a href="#" onclick="document.forms[0].submit();return false;">Let's go!</a>
        </form>
    </body>
</html>

And the view:

和观点:

def index(request):
    if request.method == 'POST':
        try:
            flag = request.POST['flag']
            # TODO use flag
        except KeyError:
            print 'Where is my flag?'

    return render_to_response('index.html', {},
        context_instance=RequestContext(request))

#2


3  

You can use AJAX to get rid of forms entirely.

您可以使用AJAX完全删除表单。

Just add this to your JavaScript:

只需将其添加到JavaScript中:

function postTo(url, query) {
    var request = (XMLHttpRequest?new XMLHttpRequest():new ActiveXObject());
    request.open('POST', url, true);
    request.send(query);
}

Then call with something like this:

然后像这样打电话:

postTo('/admin/usermanagement/','flag=2');

Note that this will NOT reload the page. If you want to reload the page, use Borges' answer.

注意,这不会重新加载页面。如果您想重新加载页面,请使用Borges的答案。