0๐
โ
I finally made it work (based on django doc and doniyor comments)
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;
}
function Favorite(item) {
song_id = item.getAttribute("data-mp3"),
csrftoken = getCookie('csrftoken');
$.ajax({
type : "POST",
datatype: "json",
url: "/fav/",
data: {
song_id : song_id
},
headers: {
'X-CSRFToken': csrftoken
}
});
return false;
}
๐คNab Ilovich
1๐
first of all, you dont need <form>
at all if you are sending the request with ajax.
secondly, you can set the csrf_token
also in this way:
...
data: {
csrfmiddlewaretoken: '{{ csrf_token }}',
song_id : song_id
},
...
which always works for me.
๐คdoniyor
- [Answer]-Trying to make a Django / Mezzanine "Page last updated by on date"
- [Answer]-Get the objects of model refrenced in another model as fk django
- [Answer]-Django ajax uploader example
- [Answer]-Django recursive menu
- [Answer]-Cookie does not show up in Chrome Developer Tools (Django Python)
Source:stackexchange.com