程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django framework Ajax

編輯:Python

List of articles

Chapter one django Installation and introduction

Chapter two django Based on using

The third chapter The routing layer

Chapter four A virtual environment 、django Version difference 、 View layer

The fifth chapter Formwork layer

Chapter six The model layer ( On )

Chapter vii. The model layer ( Next )

Chapter viii. ajax


List of articles

  • List of articles
  • One .ajax brief introduction
    • ajax Use
  • Two 、 Front and back end transmission data encoding format
  • 3、 ... and 、ajax send out json Format data
  • Four 、ajax Carry file data
  • 5、 ... and 、 Callback mechanism


One .ajax brief introduction

When the page is not refreshed, it can interact with the back end to realize asynchronous submission and local refresh

ajax Not a new knowledge The essence is some js Code We learn ajax Use it directly jQuery The version after encapsulation ( The grammar is simpler ) Use ajax The premise of must be introduced jQuery file

ajax Use

$('#btn').click(function () {

// Get the data in the two boxes
let i1Val = $('#i1').val();
let i2Val = $('#i2').val();
// send out ajax Request to transmit data
$.ajax({

url:'', // If you don't write, the default is the address of the current page
type:'post', // Specify the current request method
data:{
'i1':i1Val,'i2':i2Val}, // Request data to carry
success:function (args) {
 // Asynchronous callback function There is a reply on the back end, which is automatically triggered
$('#i3').val(args)
}
})
})

Two 、 Front and back end transmission data encoding format

form The default sending format of the form :
Content-Type: application/x-www-form-urlencoded
data format :username=jason&password=123
django The back end will automatically process to :request.POST

form Form send file :
Content-Type: multipart/form-data
data format : Hide from view
django The back end will process automatically :request.POST request.FILES

ajax Default encoding format :
Content-Type: application/x-www-form-urlencoded
data format :username=jason&password=123
django The back end will automatically process to :request.POST


3、 ... and 、ajax send out json Format data

ajax The type of data sent must be consistent with the coding format of the data

The encoding format is urlencoded
The data format should be username=jason&password=123
But you sent json Format data

django Back end access json Format data will not be processed It's just request.body Inside
We need to deal with it ourselves

$('#d1').click(function () {

$.ajax({

url:'',
type:'post', // Don't write default get request
contentType:'application/json', // Do not write default is urlencoded code
data:JSON.stringify({
'name':'jason','pwd':123}), // Serialization method
success:function (args) {

}
})
})

Four 、ajax Carry file data

$("#d1").click(function () {

// Need to use built-in js Built-in objects FormData
let myFormData = new FormData();
// Object to add normal data
myFormData.append('username',$('#name').val())
myFormData.append('password',$('#pwd').val())
// Object to add file data
myFormData.append('my_file',$('#file')[0].files[0])
// send out ajax request
$.ajax({

url:'',
type:'post',
data:myFormData,
// Carry the two parameters that must be specified in the file
contentType:false,
processData:false,
success:function (args) {

// Handle the results returned by the asynchronous callback
}
})
})

5、 ... and 、 Callback mechanism

Use ajax Interaction All operations will no longer directly affect the entire page ( Local operation )
Need to be in success Callback function to complete the subsequent operations we need


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved