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

yipeiwu_com5年前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请求的两种方法,希望对大家有所帮助!

相关文章

python3利用tcp实现文件夹远程传输

本文实例为大家分享了python实现文件夹远程传输的具体代码,供大家参考,具体内容如下 实现功能: 通过tcp协议模拟文件夹的下载,注意是文件夹,不是文件。 下面让我们实现以下: 其中...

python调用pyaudio使用麦克风录制wav声音文件的教程

python的pyaudio可以进行录音,播放,生成wav文件等等,WAVE是录音时用的标准的WINDOWS文件格式,文件的扩展名为WAV,数据本身的格式为PCM或压缩型,属于无损音乐格...

python获取交互式ssh shell的方法

更新,最近在学unix环境编程,了解一下进程的创建过程,用最原始的方式实现了一个ssh命令的执行。 #coding=utf8 ''' 用python实现了一个简单的shell,了...

python微信跳一跳系列之棋子定位像素遍历

python微信跳一跳系列之棋子定位像素遍历

前言 在前几篇博客中,分别就棋子的颜色识别、模板匹配等定位方式进行了介绍和实践,这一篇博客就来验证一下github中最热门的跳一跳外挂中采用的像素遍历的方法。 方法说明 像素遍历的实质依...

python生成式的send()方法(详解)

随便在网上找了找,感觉都是讲半天讲不清楚,这里写一下。 def generator(): while True: receive=yield 1 print('e...