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

作者:xsan 时间:2022-06-21 07:06:51 

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

来源:https://www.cnblogs.com/xshan/archive/2020/01/05/12151606.html

标签:django,ajax,post,请求
0
投稿

猜你喜欢

  • 利用机器学习预测房价

    2023-08-10 21:25:50
  • IIS上设置301跳转,实现ASP带参数跳转

    2011-04-08 12:52:00
  • Orcas中C#语言的新特性:自动属性,对象初始化器,和集合初始化器

    2007-09-23 12:43:00
  • 程序员的七种武器

    2008-11-01 17:13:00
  • thinkphp3查询mssql数据库乱码解决方法分享

    2023-11-15 00:52:22
  • 在ASP.NET 2.0中操作数据之二十五:大数据量时提高分页的效率

    2023-07-11 09:05:31
  • django自带的权限管理Permission用法说明

    2023-07-25 04:18:06
  • php中Array2xml类实现数组转化成XML实例

    2023-07-14 21:48:13
  • Python绑定方法与非绑定方法详解

    2021-04-12 00:20:19
  • python代码如何实现余弦相似性计算

    2021-08-15 03:40:04
  • python实现按键精灵找色点击功能教程,使用pywin32和Pillow库

    2023-11-08 18:30:34
  • img标签中alt和title属性的正确使用

    2008-01-10 12:59:00
  • asp模块化分页源码

    2008-04-13 07:02:00
  • python中的插值 scipy-interp的实现代码

    2022-02-23 02:50:09
  • asp防止盗链HTTP_REFERER判断代码

    2010-03-12 10:41:00
  • Mysql数据库常用命令

    2009-03-06 14:29:00
  • 在Oracle 8x实现自动断开后再连接

    2010-07-26 13:03:00
  • ASP技巧:禁用清除页面缓存的五种方法

    2009-07-21 12:33:00
  • 将不规则的Python多维数组拉平到一维的方法实现

    2023-11-05 19:47:15
  • 如何用METADATA替换ADOVBS.INC?

    2010-06-12 12:54:00
  • asp之家 网络编程 m.aspxhome.com