django ajax发送post请求的两种方法
更新时间:2020年01月05日 11:11:31 作者:xsan
这篇文章主要介绍了django ajax发送post请求的两种方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
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请求的两种方法,希望对大家有所帮助!
相关文章
tensor.squeeze函数和tensor.unsqueeze函数的使用详解
本文主要介绍了tensor.squeeze函数和tensor.unsqueeze函数的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-03-03使用Anaconda创建Python指定版本的虚拟环境的教程详解
由于工作的需要和学习的需要,需要创建不同Python版本的虚拟环境,所以这篇文章主要为大家详细介绍了如何使用Anaconda创建Python指定版本的虚拟环境,需要的可以参考下2024-03-03Python3爬虫爬取英雄联盟高清桌面壁纸功能示例【基于Scrapy框架】
这篇文章主要介绍了Python3爬虫爬取英雄联盟高清桌面壁纸功能,结合实例形式分析了基于Scrapy爬虫框架进行图片爬取的相关项目创建、文件结构、功能实现操作技巧与注意事项,需要的朋友可以参考下2018-12-12
最新评论