相关疑难解决方法(0)

Django CSRF检查失败,出现Ajax POST请求

我可以通过我的AJAX帖子使用一些符合Django的CSRF保护机制的帮助.我按照这里的指示:

http://docs.djangoproject.com/en/dev/ref/contrib/csrf/

我已经完全复制了他们在该页面上的AJAX示例代码:

http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax

我getCookie('csrftoken')在xhr.setRequestHeader打电话之前打印了一个警告打印内容,确实填充了一些数据.我不确定如何验证令牌是否正确,但我鼓励它发现并发送一些东西.

但是Django仍然拒绝我的AJAX帖子.

这是我的JavaScript:

$.post("/memorize/", data, function (result) {
    if (result != "failure") {
        get_random_card();
    }
    else {
        alert("Failed to save card data.");
    }
});
Run Code Online (Sandbox Code Playgroud)

这是我从Django看到的错误:

[23/Feb/2011 22:08:29]"POST/memorize/HTTP/1.1"403 2332

我确定我错过了一些东西,也许这很简单,但我不知道它是什么.我一直在搜索SO,并看到一些关于通过csrf_exempt装饰器关闭CSRF检查我的视图的信息,但我发现它没有吸引力.我已经试过了,但它确实有效,但我宁愿让我的POST以Django的设计方式工作,如果可能的话.

为了防止它有用,这里是我的观点的主旨:

def myview(request):

    profile = request.user.profile

    if request.method == 'POST':
        """
        Process the post...
        """
        return HttpResponseRedirect('/memorize/')
    else: # request.method == 'GET'

        ajax = request.GET.has_key('ajax')

        """
        Some irrelevent code...
        """

        if ajax:
            response = HttpResponse()
            profile.get_stack_json(response)
            return response
        else:
            """
            Get …
Run Code Online (Sandbox Code Playgroud)

python django ajax csrf

175
推荐指数
10
解决办法
14万
查看次数

Django框架中的Ajax Post?

我在django框架中对ajax/jquery文章进行了一个简单的测试,但是并不真正理解为什么输出没有进入模板页面.任何人?

我可以在firebug的"响应"选项卡中看到帖子的内容,但是我是否尝试返回模板或简单消息,浏览器本身没有任何反应.相反,非ajax帖子按预期工作(加载新页面,发布消息)

我是ajax/jquery/django的完全新手所以请原谅我的无知:)

最终,我希望能够通过jquery将任意非形式变量传递给django视图.可能?谢谢 :)

这是代码 -

的test.html:

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script></javascript>
<script type="text/javascript">
   $(document).ready(function() {
       $("#testForm").submit(function(event){
           event.preventDefault();
           $.ajax({
                type:"POST",
                url:"/test_results/"
                    });
       });
       return false;
    });
</script>
</head>
<body>
        <form id="testForm" action="/test_results/" method="post">
            <input type="submit" id="go" name="go" value="Go!">
        </form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

views.py:

from django.shortcuts import render_to_response
from django.http import HttpResponse


    def test_ajax(request):
        if request.is_ajax():
            message = "Yes, AJAX!"
        else:
            message = "Not Ajax"
        return HttpResponse(message)
        #alternative test: return render_to_response('test_results.html')
Run Code Online (Sandbox Code Playgroud)

urls.py:

(r'^test_results/$', views.test_ajax),
(r'^test/$', views.test),
Run Code Online (Sandbox Code Playgroud)

几乎是空的test_results.html:

<html>
<head>
</head>
<body> …
Run Code Online (Sandbox Code Playgroud)

django ajax jquery

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

ajax ×2

django ×2

csrf ×1

jquery ×1

python ×1