Cookies not working with an AJAX call from jQuery to Django

半城伤御伤魂 提交于 2019-12-11 15:36:45

问题


I have a Django site using a 5-star rating system for voting (I use django-ratings) and I would like to store the votings of the users with AJAX calls.

On the client side I have a JavaScript function sending a GET request to a URL:

$.ajax({
    url: url,
    success: function(data) {
        alert('Load was performed.');
    }
});

On the server side I have code setting the cookie:

def vote(request, slug, rating):

    # Some irrelevant code...
    response = HttpResponse('Vote changed.')
    response.set_cookie('vote', 123456)
    return response

The problem is that the cookie is never set in the browser.

What I am doing wrong?

Thanks!


回答1:


Are sure that your problem is about Cross-site request forgery protection? most ajax requests rejected django by that. Don't you have any error messages?



来源:https://stackoverflow.com/questions/8378340/cookies-not-working-with-an-ajax-call-from-jquery-to-django

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