# JS序列化 a = {"k1":"v1"} #序列化為字符串 類似python json.dumps(a) b = JSON.stringify(a) "{\"k1\":\"v1\"}" #序列化為字典 類似python json.loads(b) c = JSON.parse(b) Object { k1: "v1" }
1.
Python序列化
字符串 = json.dumps(對象) 對象->字符串
對象 = json.loads(字符串) 字符串->對象
JavaScript:
字符串 = JSON.stringify(對象) 對象->字符串
對象 = JSON.parse(字符串) 字符串->對象
應用場景:
數據傳輸時,
發送:字符串
接收:字符串 -> 對象
2. ajax
$.ajax({
url: 'http//www.baidu.com',
type: 'GET',
data: {'k1':'v1'},
success:function(arg){
// arg是字符串類型
// var obj = JSON.parse(arg)
}
})
$.ajax({
url: 'http//www.baidu.com',
type: 'GET',
data: {'k1':'v1'},
dataType: 'JSON',
success:function(arg){
// arg是對象
}
})
$.ajax({
url: 'http//www.baidu.com',
type: 'GET',
data: {'k1':[1,2,3,4]},
dataType: 'JSON',
success:function(arg){
// arg是對象
}
})
發送數據時:
data中的v
a. 只是字符串或數字
$.ajax({
url: 'http//www.baidu.com',
type: 'GET',
data: {'k1':'v1'},
dataType: 'JSON',
success:function(arg){
// arg是對象
}
})
b. 包含屬組
$.ajax({
url: 'http//www.baidu.com',
type: 'GET',
data: {'k1':[1,2,3,4]},
dataType: 'JSON',
traditional: true,
success:function(arg){
// arg是對象
}
})
c. 傳字典
b. 包含屬組
$.ajax({
url: 'http//www.baidu.com',
type: 'GET',
data: {'k1': JSON.stringify({}) },
dataType: 'JSON',
success:function(arg){
// arg是對象
}
})
d.傳整個form表單
$(function () {
ajaxsumbit();
});
function ajaxsumbit() {
$("#btn").click(function () {
$.ajax({
url: "ajax1.html",
data: $("#fm").serialize(), # form表單內容
type: "GET",
dataType: "json",
success: function (args) {
console.log(args.username);
console.log(args.passwd);
console.log(args.status);
$("#sp").text(args.status + " " + args.passwd + " " + args.username);
},
error: function () {
alert("error")
}
})
})
}
3. 事件委托
$('要綁定標簽的上級標簽').on('click','要綁定的標簽',function(){})
$('要綁定標簽的上級標簽').delegate('要綁定的標簽','click',function(){})
4. 編輯
5. 總結
新URL方式:
- 獨立的頁面
- 數據量大或條目多
對話框方式:
- 數據量小或條目少
-增加,編輯
- Ajax: 考慮,當前頁;td中自定義屬性
- 頁面(***)
刪除:
對話框