計劃把 文章全部轉移至 這裡,而本 Blogger 站台的文章近 500 篇,我預計在 2014-12-31 前移轉完畢,完成後 將只作代轉服務,一律把舊連結如 轉成 ,敬請舊雨新知互相走告。


何岳峰 敬上

2009年3月4日 星期三

return HttpResponseRedirect with Post Data?

This is an example to solve the question from Django User Group

In the specification of http status code 302, there are no way to pass a post data to user's browser. But we can use AJAX method to reach this requirement. The principle is using the AJAX request to do some thing must run in the server and return a json data to browser, then the callback function that defined in the html will shape a html form and submit this form.

def readReturnHttpRedirectWithPost(R):
if R.GET.get('var', None):
# do something what you want
return HttpResponse(json.write({'action': 'https://your.web.sitek/', 'vars': {'var1': 'var1', 'var2': 'var2'}}))
html = """
    <html><head><script type="text/javascript" src="/media/jquery.ui-1.5.1/jquery-1.2.6.js"></script></head>
          <ul><li><input type="text" id="var"></li>
              <li><input type="submit" id="updateFormWithPost"></li></ul>
          <form id="post_form" method="POST"></form>
                    var value = $('#var').val();
                    $.getJSON('/u/readreturnhttpredirectwithpost/', {'var': value}, function(json){
                        var $post_form = $('#post_form');
                        $post_form.attr('action', json['action']);
                        for (var i in json['vars']){
                            var $input = $('<input type="hidden" name="'+i+'" value="'+json['vars'][i]+'">');
return HttpResponse(html)

1 則留言:

  1. That's fine and dandy if the browser has javascript enabled. But I use NoScript all the time, your AJAX version won't work for me....



Related Posts Plugin for WordPress, Blogger...