ajax同步与异步

前端之家收集整理的这篇文章主要介绍了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/',arg: "/bin/mkdir /root/jtest",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/',arg: "/bin/mkdir /root/jtest2",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,c);
    });

    return result;

});
原文链接:https://www.f2er.com/ajax/162985.html

猜你在找的Ajax相关文章