1.后端代码编写
from django.http import JsonResponse
if True: message = {"code":0,"msg":"同步公有云机器成功,刷新页面获取最新数据!"} else: message = {"code":1,"msg":"同步公有云机器失败,项目配置中还没有配置[项目云id]"} return JsonResponse(message)
2.前端ajax提交接收返回数据
$('#get_tencentcloud_instances').click(function () { $.ajax({ url: "{{ request_url }}", type: "POST", data: $('#form_data_edit').serialize(), beforeSend: function (xhr,settings) { xhr.setRequestHeader("X-CSRFtoken",$.cookie("csrftoken")); layer.msg('同步数据中,请等待......', {icon: 4,time:600000}); }, success: function (data) { if (data.code == 0){ layer.alert(data.msg, {icon: 6}); } else { layer.alert(data.msg, {icon: 7}); } }, complete: function () { console.info("同步数据执行完成......"); }, error: function (data) { console.info("同步数据执行出错......"); } }); })
$('#get_tencentcloud_instances').click(function () {
$.ajax({
url: "{{ request_url }}",
type: "POST",
data: $('#form_data_edit').serialize(),
beforeSend: function (xhr,settings) {
xhr.setRequestHeader("X-CSRFtoken",$.cookie("csrftoken"));
layer.msg('同步数据中,请等待......', {icon: 4,time:600000});
},
success: function (data) {
if (data.code == 0){
layer.alert(data.msg, {icon: 6});
} else {
layer.alert(data.msg, {icon: 7});
}
},
complete: function () {
console.info("同步数据执行完成......");
},
error: function (data) {
console.info("同步数据执行出错......");
}
});
})