weixin_33725126 2016-11-17 08:42 采纳率: 0%
浏览 39

CSRF Coo​​kie未设置错误

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)

  • 写回答

1条回答 默认 最新

  • weixin_33721427 2016-11-17 08:58
    关注

    Just change CSRF_COOKIE_SECURE = True to CSRF_COOKIE_SECURE = False in settings.py

    Please See:

    Question was already been answered to below link

    Django-Set-Cookie

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘