正如标题所述,我的(Django)CSRF验证在Chrome中运行但不在Firefox中,我想知道为什么我可以解决这个问题.
我将其包含在我的base.html文件的head标记中,我的应用程序中的所有其他文件都从该标记扩展:
<script>
$(document).ready(function() {
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getCookie('csrftoken');
function …Run Code Online (Sandbox Code Playgroud) 我需要删除"u"前缀,因为我将这些json序列化列表传递到前端并使用javascript处理它们.Javascript无法理解这些"你".
这是代码:
context['list_of_dicts'] = serialize('json', my_list_of_dicts)
# this function is wrapped with a @json response decorator
Run Code Online (Sandbox Code Playgroud)
@json_response看起来像:
def json_response(func):
"""
A decorator thats takes a view response and turns it
into json. If a callback is added through GET or POST
the response is JSONP.
"""
def decorator(request, *args, **kwargs):
objects = func(request, *args, **kwargs)
if isinstance(objects, HttpResponse):
return objects
try:
data = simplejson.dumps(objects)
if 'callback' in request.REQUEST:
# a jsonp response!
data = '%s(%s);' % (request.REQUEST['callback'], data)
return HttpResponse(data, …Run Code Online (Sandbox Code Playgroud) django ×2
ajax ×1
django-csrf ×1
firefox ×1
javascript ×1
jquery ×1
json ×1
python ×1
unicode ×1