django ajax发送post请求的两种方法

yipeiwu_com6年前Python基础

django ajax发送post请求的两种方法,具体内容如下所述:

第一种:将csrf_token放在from表单里

 <script>
    function add_competion_goods() {
      $.ajax({
        url: "{% url 'add_competition_goods' %}",
        type: "POST",
        dataType: "json",
        data: $('#add_competition_goods_from').serialize(),//直接将from表单打包
        success: function () {
          $('#add_competition_modal').modal('hide');
          alert('secces')
        }
      })
    }
  </script>

   第二种:发送前添加头部信息

<script>
    function submit_read_save_order_data() {
      var excel_file = document.getElementById("order_excel").files;
      var excel_file_size = excel_file[0]['size'];
      console.log(excel_file_size);
      if (excel_file_size > 0 & excel_file_size < 60000000) {
        alert("已开始上传");
        $('button#upload_data').attr('disabled', 'disabled');
        {#console.log(excel_file_size);#}
        var fd = new FormData();
        fd.append('excels', excel_file[0]);
        $.ajax({
            url: "{%url 'read_save_order_data' %}",
            type: "POST",
            dataType: "json",
            data: fd,
            processData: false,// tell jQuery not to process the data
            contentType: false,// tell jQuery not to set contentType
            beforeSend: function (xhr, setting) {
              xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}")
            },
            success: function (msg) {
              alert(msg)
            },
            error: function (msg) {
              alert(msg)
             }
          }
        )
      } else {
        alert("文件为空,或大小超出60M,请检查")
      }
    }
  </script>

总结

以上所述是小编给大家介绍的django ajax发送post请求的两种方法,希望对大家有所帮助!

相关文章

python中format()函数的简单使用教程

python中format()函数的简单使用教程

先给大家介绍下python中format函数,在文章下面给大家介绍python.format()函数的简单使用 ---恢复内容开始--- python中format函数用于字符串的格式化...

解决tensorflow1.x版本加载saver.restore目录报错的问题

这个错误是最新的错误哈,目前只在tensorflow上的github仓库上面有提出,所以你在百度上面找不到。 是个tensorflow的bug十天前提出的,只有github仓库上一个地方...

Python+Django搭建自己的blog网站

Python+Django搭建自己的blog网站

一、前言 1.1.环境 python版本:3.6 Django版本:1.11.6 1.2.预览效果 最终搭建的blog的样子,基本上满足需求了。框架搭好了,至于CSS,可以根据自己喜好随...

关于python写入文件自动换行的问题

现在需要一个写文件方法,将selenium的脚本运行结果写入test_result.log文件中 首先创建写入方法 def write_result(str): writeres...

Python输出PowerPoint(ppt)文件中全部文字信息的方法

本文实例讲述了Python输出PowerPoint(ppt)文件中全部文字信息的方法。分享给大家供大家参考。具体分析如下: 下面的代码依赖于windows com,所以必须在机器上安装P...