django學(xué)習(xí)之a(chǎn)jax post傳參的2種格式實(shí)例
一.ajax介紹
1、ajax的含義
Ajax全稱“Async Javascript And XML”即:異步的javascript和XML。它是一種稱謂,并不指代某項(xiàng)具體的技術(shù),準(zhǔn)確來說是一系列技術(shù)的集合.現(xiàn)在,所有的無刷新操作都被稱為“Ajax”.
2、使用ajax的好處:
使用ajax避免了整頁數(shù)據(jù)的刷新,也減少了請求等待的時(shí)間,提高了用戶體驗(yàn).
二.ajax傳參的兩種格式
假設(shè)有如下表單,需要將這些表單用ajax傳參的方式傳給后臺(tái),該怎么做呢…

我們知道ajax傳參的格式為$.post(“地址”,參數(shù),function(返回值){}),套用這個(gè)格式進(jìn)行傳參,有以下兩種方法:
方法一:提交表單中的部分字段
我們可以獲取用戶名,密碼等內(nèi)容,將其拼接成一個(gè)字典(想傳什么就將其拼接成字典格式,沒有特殊限制,你甚至可以單獨(dú)的只傳一個(gè)用戶名),將其作為參數(shù)傳給后臺(tái)
例:
{‘username':username,‘password':password,‘csrfmiddlewaretoken':csrf}
或
{‘username':username‘}
或
{‘password':password}
關(guān)于csrf是預(yù)防跨站攻擊的內(nèi)容,你可以移步djanjo之csrf防跨站攻擊作下了解
接下來看看代碼中是如何實(shí)現(xiàn)的,重點(diǎn)關(guān)注帶有下方標(biāo)記的代碼
{# 🌈ajax #}
{# 🌈post提交 #}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注冊</title>
{# 引用jquery #}
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<form ation="" method="post">
{# 防止跨站攻擊 #}
{% csrf_token %}
用戶名:<input type="text" name="username"><br>
密碼:<input type="text" name="password"><br>
<!-- {# 表單提交 #}-->
<!-- <input type="submit">-->
<!-- {# ajax提交 #}-->
<input type="button" value="注冊" id="button">
</form>
</body>
</html>
<script>
{# 🌈ajax #}
$("#button").click(function(){
username = $("[name='username']").val();
password = $("[name='password']").val();
csrf = $("[type='hidden']").val();
console.log(username,password,csrf);
{# 🌈post提交 #}
{# $.post("地址",{參數(shù)},function(返回值){}) #}
$.post("/user/register/",{'username':username,'password':password,'csrfmiddlewaretoken':csrf},function(data){
console.log(data)
})
});
</script>
方法二:提交表單中的所有字段
console.log($(“form”).serialize()
serialize是把表單中的字段序列化,弄成get的請求的字符串格式,將其作為參數(shù)傳給后臺(tái)
值得注意的是這里就不能像方法一里那樣想傳什么參數(shù)就傳什么參數(shù)了,而是表單中所有的字段都會(huì)被納為請求的字符串格式
接下來看看代碼中是如何實(shí)現(xiàn)的,重點(diǎn)關(guān)注帶有下方標(biāo)記的代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注冊</title>
{# 引用jquery #}
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<form ation="" method="post">
{# 防止跨站攻擊 #}
{% csrf_token %}
用戶名:<input type="text" name="username"><br>
密碼:<input type="text" name="password"><br>
<!-- {# 表單提交 #}-->
<!-- <input type="submit">-->
<!-- {# ajax提交 #}-->
<input type="button" value="注冊" id="button">
</form>
</body>
</html>
<script>
{# 🌈ajax #}
$("#button").click(function(){
console.log($("form").serialize());
{# 🌈post提交 #}
{# $.post("地址",{參數(shù)},function(返回值){}) #}
$.post("/user/register/",console.log($("form").serialize()),function(data){
console.log(data)
})
});
</script>
總結(jié)
到此這篇關(guān)于django學(xué)習(xí)之a(chǎn)jax post傳參的文章就介紹到這了,更多相關(guān)django之a(chǎn)jax post傳參內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python+matplotlib繪制簡單的海豚(頂點(diǎn)和節(jié)點(diǎn)的操作)
這篇文章主要介紹了python+matplotlib繪制簡單的海豚(頂點(diǎn)和節(jié)點(diǎn)的操作),具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
Python實(shí)現(xiàn)數(shù)值積分方式
今天小編就為大家分享一篇Python實(shí)現(xiàn)數(shù)值積分方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
python3 使用traceback定位異常實(shí)例
這篇文章主要介紹了python3 使用traceback定位異常實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
在win64上使用bypy進(jìn)行百度網(wǎng)盤文件上傳功能
這篇文章主要介紹了在win64上使用bypy進(jìn)行百度網(wǎng)盤文件上傳功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
python3 實(shí)現(xiàn)爬取TOP500的音樂信息并存儲(chǔ)到mongoDB數(shù)據(jù)庫中
今天小編就為大家分享一篇python3 實(shí)現(xiàn)爬取TOP500的音樂信息并存儲(chǔ)到mongoDB數(shù)據(jù)庫中,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08

