django ajax發(fā)送post請求的兩種方法
更新時間:2020年01月05日 11:11:31 作者:xsan
這篇文章主要介紹了django ajax發(fā)送post請求的兩種方法,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
django ajax發(fā)送post請求的兩種方法,具體內(nèi)容如下所述:
第一種:將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>
第二種:發(fā)送前添加頭部信息
<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>
總結(jié)
以上所述是小編給大家介紹的django ajax發(fā)送post請求的兩種方法,希望對大家有所幫助!
相關(guān)文章
高考要來啦!用Python爬取歷年高考數(shù)據(jù)并分析
轉(zhuǎn)眼間,高考的日子又要來臨了,不知道高考學(xué)子們準(zhǔn)備的怎么樣了,今天這篇文章簡單且隨意地分析一下高考的一些數(shù)據(jù),需要的朋友可以參考下2021-06-06
Python學(xué)習(xí)筆記之集合的概念和簡單使用示例
這篇文章主要介紹了Python學(xué)習(xí)筆記之集合的概念和簡單使用,涉及Python集合的定義、查找、添加、刪除等相關(guān)操作技巧與注意事項,需要的朋友可以參考下2019-08-08
一篇文章徹底搞懂Python中可迭代(Iterable)、迭代器(Iterator)與生成器(Generator)的概念
這篇文章主要給大家介紹了如何通過一篇文章徹底搞懂Python中可迭代(Iterable)、迭代器(Iterator)與生成器(Generator)的概念,對大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
python中redis查看剩余過期時間及用正則通配符批量刪除key的方法
這篇文章主要介紹了python中redis查看剩余過期時間及用正則通配符批量刪除key的方法,需要的朋友可以參考下2018-07-07
使用Python和Selenium構(gòu)建一個自動化圖像引擎
這篇文章主要為大家詳細(xì)介紹了如何使用Python和Selenium庫構(gòu)建一個自動化圖像引擎,能夠根據(jù)指定參數(shù)自動截取網(wǎng)頁快照,并將生成的圖片存儲到云端,需要的可以參考下2024-12-12

