我使用Ajax发送POST数据来查看Django但我有这个错误:403 FORBIDDEN。
实际上,我使用@csrf_exempt但是我害怕不是更好的主意......
我的观点:
@csrf_exempt
def myfic(request):
if request.method == "POST" :
competences = request.POST.get('theCompetences')
print("competences : ",competences)
...
我的代码JS:
function envoie_post_competences(){
$.post("http://localhost:8000/myfic",{theCompetences:"aaaaaaa",});
return false;
}
什么是更好的解决方案?
感谢您的帮助!
所以我必须添加此代码:
var csrftoken = $.cookie('csrftoken');
function csrfSafeMethod(method) {
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
我的函数JS没有改变?
function envoie_post_competences(){
$.post("http://localhost:8000/myfic",{theCompetences:"aaaaaaa",});
return false;
}
如果是,当我点击提交按钮时,什么都没发生..