ajax同步與異步

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;

});
發佈了98 篇原創文章 · 獲贊 46 · 訪問量 26萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章