django ajax發(fā)送post請(qǐng)求的兩種方法
更新時(shí)間:2020年01月05日 11:11:31 作者:xsan
這篇文章主要介紹了django ajax發(fā)送post請(qǐng)求的兩種方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
django ajax發(fā)送post請(qǐng)求的兩種方法,具體內(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,請(qǐng)檢查")
}
}
</script>
總結(jié)
以上所述是小編給大家介紹的django ajax發(fā)送post請(qǐng)求的兩種方法,希望對(duì)大家有所幫助!
相關(guān)文章
Python使用pylab庫實(shí)現(xiàn)畫線功能的方法詳解
這篇文章主要介紹了Python使用pylab庫實(shí)現(xiàn)畫線功能的方法,結(jié)合具體實(shí)例分析了Python使用pylab庫的相關(guān)函數(shù)實(shí)現(xiàn)畫線功能的操作技巧,并附帶說明了相關(guān)函數(shù)與參數(shù)功能,需要的朋友可以參考下2017-06-06
Python實(shí)現(xiàn)將字典(列表按列)存入csv文件
這篇文章主要介紹了Python實(shí)現(xiàn)將字典(列表按列)存入csv文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
python之隨機(jī)數(shù)函數(shù)的實(shí)現(xiàn)示例
這篇文章主要介紹了python之隨機(jī)數(shù)函數(shù)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
用Python爬取LOL所有的英雄信息以及英雄皮膚的示例代碼
這篇文章主要介紹了用Python爬取LOL所有的英雄信息以及英雄皮膚的示例代碼,主要分為兩部分,獲取網(wǎng)頁上數(shù)據(jù)和圖片保存到本地等,感興趣的可以了解一下2020-07-07

