Javascript 如何等待Ajax成功:函数成功下一步工作

Javascript 如何等待Ajax成功:函数成功下一步工作,javascript,jquery,ajax,Javascript,Jquery,Ajax,无法确定Ajax成功的条件: function GetProm(){ var checkmoket = false; $.ajax({ type: 'POST', url: url, data: { domain: b }, cache: false, success: function (data)

无法确定Ajax成功的条件:

 function GetProm(){ 
       var checkmoket = false;
     $.ajax({
                type: 'POST',
                url: url,
                data: { domain: b },
                cache: false,
                success: function (data)
                {
                  var listProm = data.ListResult;
                  if (flagInside === 0)
                  for (var i = 0; i < listProm.length; i++) 
                  {
                    if (listProm[i].ID === 384 || listProm[i].ID === 686) 
                    {
                      checkmoket = true; // => here
                    }
                  }
                 }
           });
        if(checkmoket) // => checkmoket always is false
           return;
    }
函数GetProm(){code> var checkmoket=false; $.ajax({ 键入:“POST”, url:url, 数据:{域:b}, cache:false, 成功:功能(数据) { var listProm=data.ListResult; 如果(flagInside==0) 对于(变量i=0;i此处 } } } }); 如果(checkmoket)/=>checkmoket始终为false 回来 } 为什么
checkmoket
总是为false?
如果条件正确,我想
checkmoket
是否为真?

您必须等待ajax完成,您可以尝试回调,如下所示:

    function ajax(method, url, data, callback) {
        var obj = {
            type: method,
            url: window.location.origin + url,
            contentType: "application/json; charset=utf-8",
            success: function (a) {
                callback(a);
            }, error: function (err) {
                if ((err.responseJSON).constructor == Object)
                    Object.keys(err.responseJSON).length || (err.responseJSON = {'error': err.statusText});
                else if ((err.responseJSON).constructor == Array)
                    err.responseJSON.length || (err.responseJSON = {'error': err.statusText});
                callback(err.responseJSON);
            }
        };
        data && (obj.data = JSON.stringify(data));
        $.ajax(obj);
    }


    ajax('GET', '/api/data', {}, function (a) {
        if (a.error)
            $.growl.error({message: a.error});
        else {
            //do somthing
        }
    });

尝试以下我添加的代码
async:false
,您的函数签名错误是
函数GetProm{
我更正为
函数GetProm(){

函数GetProm(){code> var checkmoket=false; $.ajax({ 键入:“POST”, url:url, 数据:{域:b}, cache:false, async:false,//添加了新属性。 成功:功能(数据) { var listProm=data.ListResult; 如果(flagInside==0) 对于(变量i=0;i此处 } } } }); 如果(checkmoket)/=>checkmoket始终为false 回来 }
是否使用真值执行条件?
function GetProm(){ 
    var checkmoket = false;
    $.ajax({
        type: 'POST',
        url: url,
        data: { domain: b },
        cache: false,
        async:false, // New properties added.
        success: function (data)
        {
          var listProm = data.ListResult;
          if (flagInside === 0)
          for (var i = 0; i < listProm.length; i++) 
          {
            if (listProm[i].ID === 384 || listProm[i].ID === 686) 
            {
              checkmoket = true; // => here
            }
          }
         }
       });
       if(checkmoket) // => checkmoket always is false
       return;
    }