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实现用户登陆邮件通知的方法

本文实例讲述了python实现用户登陆邮件通知的方法。分享给大家供大家参考。具体如下: 这里写在linux计划任务里定时执行,当有新用户登陆时候发送用户名到指定邮箱通知管理员。 #!...

python 并发编程 多路复用IO模型详解

python 并发编程 多路复用IO模型详解

多路复用IO(IO multiplexing) 这种IO方式为事件驱动IO(event driven IO)。 我们都知道,select/epoll的好处就在于单个进程process就...

python 使用正则表达式按照多个空格分割字符的实例

python 使用正则表达式按照多个空格分割字符的实例

程序代码如下 import os import re os.system("nmap -sP 192.168.3.0/24") //扫描IP mac = os.popen("cat...

使用Python的Django框架结合jQuery实现AJAX购物车页面

使用Python的Django框架结合jQuery实现AJAX购物车页面

Django中集成jquery 首先,静态的资源通常放入static文件夹中: static/ css/ djquery.css samples/...

Python正则表达式指南 推荐

Python正则表达式指南 推荐

本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例。本文的内容不包括如何编写高效的正则表达式、如何优化正则表达式,这些主题...