开发者

why post doesnt do anything

everyone! I'm writing a simple django application. There is script on one of the pages, which works on the button click and uses ajax post. The problem is: post method doesn开发者_StackOverflow't cause execution of server code and just executes function on error every time.

script:

<script type="text/javascript">
    (function($) {
        $(document).ready(function(){
            {% for obj in cl.result_list %}
                $('#done_{{ obj.pk }}').click(function() {
                    if ($(this).html().indexOf("icon-yes") != -1) {
                        $action = "no";
                    }
                    else {
                        $action = "yes";
                    }
                    $.ajax({
                                type: "POST",
                                url: "/on_hold_done/done/" + $action + "/{{ obj.pk }}/",
                                success: function(response) {
                                    alert("success")
                                },
                                error: function(response) {
                                    alert("error")
                                }
                            })
                });
            {% endfor %}
        });})(django.jQuery);
</script>

in urls.py:

(r"^on_hold_done/(on_hold|done)/(yes|no)/(\d*)/$", "todo.views.on_hold_done")

in views.py:

@staff_member_required
def on_hold_done(request, mode, action, pk):
    """simple code here"""
    return HttpResponse('')


If you're using Django 1.3, note that all AJAX requests are now subject to cross-site request forgery protection, so you'll need to add in some extra JS to automatically include the CSRF token in your ajax submission. See the docs here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜