I use Ajax for send in POST data to view of Django but I have this error : 403 FORBIDDEN.
Actually, I use @csrf_exempt but I'm affraid isn't the better idea...
My view :
@csrf_exempt
def myfic(request):
if request.method == "POST" :
competences = request.POST.get('theCompetences')
print("competences : ",competences)
...
My code JS :
function envoie_post_competences(){
$.post("http://localhost:8000/myfic",{theCompetences:"aaaaaaa",});
return false;
}
What's the better solution ?
Thank you for your help !
So just i must add this code :
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);
}
}
});
and my function JS not changed ?
function envoie_post_competences(){
$.post("http://localhost:8000/myfic",{theCompetences:"aaaaaaa",});
return false;
}
If yes, when I click on submit button, nothing is happening..
您需要设置X-CSRFToken
标头,如文档中所示。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.