내 jQuery
기능이 보이는jQuery를이 : 나는를 디버깅 할 때 장고
$(function() {
// activate "New" buttons if input is not empty
$('form input[type="text"]').live('keyup', function() {
var val = $.trim(this.value);
$(this).next("button").prop('disabled', val.length === 0);
});
$("body").on("submit","form",function(e){
// do not submit the form
e.preventDefault();
// handle everything yourself
var $form = $(this);
var title = $form.closest('.video-detail').find('.title').text();
var entryTitle = $form.find('.input-small').val();
console.debug(title);
console.debug(entryTitle);
$.ajaxSetup({
beforeSend: function(xhr, settings) {
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;
}
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
}
});
// send the data to the server using .ajax() or .post()
$.ajax({
type: 'POST',
url: 'addVideo',
data: {
video_title: title,
csrfmiddlewaretoken: '{{ csrf_token }}'
},
}).done(function(){
alert('done');
});
});
});
같은이가 대답을 기반으로 Django CSRF check failing with an Ajax POST request
내 html
는
<form class="new-playlist form-inline" onclick="event.stopPropagation()">{% csrf_token %}
<input type="text" class="input-small">
<button class="btn btn-danger create-playlist-button" type="submit" disabled="disabled">New</button>
</form>
처럼 보인다 파이어 폭스의 코드입니다. 게시 된 값은
로 표시됩니다.{{ csrf_token }}
값을 어떻게 채울 수 있습니까?
'getCookie ('csrftoken')'return? –