async: false,
默认是异步的,结果没返回就接着执行下面的js语句,如果下面的语句依赖ajax请求的结果那么异步明显是不行的,所以加入上面的参数,来关闭异步
后面补充一些ajax的用法
$(".btn_update").click(function(){
var tgt = $(this).parent('td').attr('ipaddress');
var from_path = $(this).parent('td').attr('from_path');
var to_path = $(this).parent('td').attr('to_path');
var result = "";
ajax_cp = $.ajax({
url:'http://127.0.0.1:8000/AUTO_DEPLOY/cp_file/',
type:'post',
dataType: "json",
data:{
tgt : tgt,
from_path :from_path,
to_path : to_path,
},
async : false,
success: function (json) {
result = "ok cp";
console.log(json)
console.log("cp ok");
console.log(tgt);
console.log(from_path);
console.log('end');
}, error: function (json) {
result = "123";
console.log(json);
UIkit.notify(json.responseJSON.return);
console.log("cp not ok");
console.log(tgt);
console.log(from_path);
console.log('end');
return False;
}
});
ajax_cmd = $.ajax({
url:'http://127.0.0.1:8000/AUTO_DEPLOY/cmd_run/',
type:'post',
dataType: "json",
data:{
tgt : tgt,
arg: "/bin/mkdir /root/jtest",
},
async : false,
success: function (json) {
result = "ok jtest";
console.log("jtest ok");
}, failure: function () {
console.log("jtest not ok");
}
});
ajax_cmd2 = $.ajax({
url:'http://127.0.0.1:8000/AUTO_DEPLOY/cmd_run/',
type:'post',
dataType: "json",
data:{
tgt : tgt,
arg: "/bin/mkdir /root/jtest2",
},
async : false,
success: function (json) {
result = "ok cmd2";
console.log("jtest2 ok");
}, failure: function () {
result = "bad";
console.log("jtest2 ok");
}
});
$.when(ajax_cp,ajax_cmd,ajax_cmd).then(function(a, b,c) {
console.log(a, b,c);
});
return result;
});