本篇文章給大家分享的是有關如何在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請求,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。