I have developed a project on django in which AJAX post request is sent to server. I have used csrf token in ajax and my project runs just fine. the problem comes when I copy my project to another machine and run it there. following error arises:
Forbidden (CSRF cookie not set.)
Both machines run just fine and have almost same configurations for django. Does anyone have any idea this problem arose?? Following is my ajax :
var csrftoken = Cookies.get('csrftoken');
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
var $chatlog = $('.js-chat-log');
var $input = $('.js-text');
var $sayButton = $('.js-say');
function createRow(text) {
var $row = $('<li class="list-group-item"></li>');
$row.text(text);
$chatlog.append($row);
}
function submitInput() {
var inputData = {
'text': $input.val()
}
// Display the user's input on the web page
createRow(inputData.text);
var $submit = $.ajax({
type: 'POST',
url: chatterbotUrl,
data: JSON.stringify(inputData),
contentType: 'application/json'
});
$submit.done(function(statement) {
createRow(statement.text);
// Clear the input field
$input.val('');
});
$submit.fail(function() {
// TODO: Handle errors
});
}
$sayButton.click(function() {
submitInput();
});
$input.keydown(function(event) {
// Submit the input when the enter button is pressed
if (event.keyCode == 13) {
submitInput();
}
});
Please note that project runs completely fine on one machine(django ver 1.10) and cant run on any other machine(ver 1.9.4)