how to send url parameter in POST request without form

若如初见. 提交于 2020-01-01 05:37:06

问题


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.

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

I want to send this hiding flag=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.


回答1:


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

<!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:


You can use AJAX to get rid of forms entirely.

Just add this to your 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.



来源:https://stackoverflow.com/questions/10773144/how-to-send-url-parameter-in-post-request-without-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!