我的ajax在jquery函数中:
Btnapplyconfig.js:应用程序配置文件
$(".btnApplyConfig").click(function(){
var token = $("input[name=csrfmiddlewaretoken]").val();
// Some other vars I'm sending properly
console.log('token: '+token); //printing correctly
$("#"+frm).submit(function(e){
e.preventDefault();
console.log('Post method via ajax');
$.ajax({
url: '/ajax/validate_config',
type: 'POST',
data: {
'token': token,
//and other stuff I'm sending properly
},
dataType: 'json',
});
});
});
我的Django观点:
def validate_config(request):
token = request.GET.get('token', None)
#some other vars I've sent ok with ajax
data = {
#some vars
'token': token,
}
if request.method == 'POST':
item = MyClass.objects.filter(my_keyword=my_filter_values).update(my_ajax_values)
return JsonResponse(data)
所有的数据都被正确处理了,唯一的问题是我得到了以下错误:
Forbidden (CSRF token missing or incorrect.): /ajax/validate_config/
我已经在视图中放置了一些指纹,以检查VAR是否正确发送,是的。
我该怎么办?
我查了一些教程,但到目前为止还找不到解决办法。