1、程序
以下程序是實驗后可以沒問題的程序
(1)前臺程序:
function savedbsource(){
//(1)讀取表單數(shù)據(jù)形成json數(shù)據(jù)
var t_dbsource_dict = {
"DBName": "test",
"UserComment": "",
"DBType": "",
"DBEdition": "",
"DBVersion": "",
"HostName": "",
"SerName": "",
"Port": "",
"UserName": "",
"Password": ""
};
t_dbsource_dict["DBName"]= $("#input_dbsourcename").val();
t_dbsource_dict["UserComment"]= $("#input_usercomment").val();
t_dbsource_dict["DBType"]= $("#input_dbsourcetype").val();
t_dbsource_dict["DBEdition"]= $("#input_dbedition").val();
t_dbsource_dict["DBVersion"]= $("#input_dbversion").val();
t_dbsource_dict["HostName"]= $("#input_hostname").val();
t_dbsource_dict["SerName"]= $("#input_database").val();
t_dbsource_dict["Port"]= $("#input_port").val();
t_dbsource_dict["UserName"]= $("#input_username").val();
t_dbsource_dict["Password"]= $("#input_password").val();
//(2)向數(shù)據(jù)庫中寫入數(shù)據(jù)
t_url_str = '/SystemConfig/DBSourceConfig/dbsourcesave';
$.ajax({
type: "POST",
url: t_url_str,
//將字典性數(shù)據(jù)轉(zhuǎn)換成json
//stringify將各種類型轉(zhuǎn)化成json字符串诅炉,parse 將json字符串專橫成obj
data: JSON.stringify(t_dbsource_dict),
dataType: "json",
async: false,
error: function () {
alert('請求失敗');
},
success: function (v_dbsourceisok) {
alert(v_dbsourceisok.dbsourceisok);
if (v_dbsourceisok.dbsourceisok == true) {
alert("添加成功氓润!");
} else {
alert("添加失斅咐帧!請重新配置……");
}
}
})
(2)后臺處理:
@SystemConfig_bp.route("/SystemConfig/DBSourceConfig/dbsourcesave",methods=["POST","GET"])
def DBSouceSave():
t_dbsource_json = request.get_data(as_text=True)
if len(t_dbsource_json) != 0:
t_dbsource_dict = json.loads(t_dbsource_json)
DBConfigOption.AppendDBSourceinfo(t_dbsource_dict)
v_dbsourceisok = {'dbsourceisok': True}
return v_dbsourceisok
else:
v_dbsourceisok = {'dbsourceisok': False}
return v_dbsourceisok