jQuery 如何使ajax同步?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19301863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:37:04  来源:igfitidea点击:

how to make ajax synchronous?

javascriptjqueryajax

提问by

How do I make function out of this?

我如何利用这个功能?

         //check if station is alive
         $.ajax({
           url: "lib/grab.php",
           data: "check_live=1&stream_url="+valueSelected,
           type: "GET",
               success: function (resp) {
                 if (resp == 1) {
                   play_this(valueSelected);
                 } else {
                   //
                 }
               },
               error: function (e) {
                 console.dir(e);
               }
         });

I thought I could do something like this:

我以为我可以做这样的事情:

function is_alive(valueSelected) {
result = false;
             //check if station is alive
             $.ajax({
               url: "lib/grab.php",
               data: "check_live=1&stream_url="+valueSelected,
               type: "GET",
                   success: function (resp) {
                     if (resp == 1) {
                       result = true;
                     } else {
                       //
                     }
                   },
                   error: function (e) {
                     console.dir(e);
                   }
             });
return result;
}

But obviously due to asynchronous nature of ajax call, result always returns false.

但显然由于ajax调用的异步性,result总是返回false。

What is the trick of dealing with this situation?

处理这种情况的诀窍是什么?

Seems to work:

似乎工作:

        //check if station is alive
        function is_alive(url) {
          //
          var result = false;
          //
          return $.ajax({
            url: "lib/grab.php",
            data: "check_live=1&stream_url="+url,
            type: "GET",
                success: function (resp) {
                  if (resp == 1) {
                    //
                    result = true;
                    //
                  }
                },
                error: function (e) {
                  console.dir(e);
                }
          }).then(function() {
            return $.Deferred(function(def) {
              def.resolveWith({},[result,url]);
            }).promise();
          });
        }

And call it like this:

并这样称呼它:

    //Change song on select, works both for fav and station lists
    $(document).on("click", ".ui-listview li a", function(){
      var valueSelected = $(this).data("station-url");
      //
      is_alive(valueSelected).done(function(result,url){
          if (result) {
            //
            play_this(valueSelected);
            //
          }
      });
    });

采纳答案by Kevin B

You don't have to make it synchronous to make it a useful function.

您不必使其同步即可使其成为有用的功能。

function is_alive(valueSelected) {
    //check if station is alive
    return $.ajax({
        url: "lib/grab.php",
        data: "check_live=1&stream_url=" + valueSelected,
        type: "GET",
        error: function (e) {
            console.dir(e);
        }
    }).then(function (resp) {
        return $.Deferred(function(def){
            def.resolveWith({},[resp == 1,valueSelected]);
        }).promise();
    });
}

is_alive(somevalue).done(function(result,valueSelected){
    alert(result);
    alert(valueSelected); 
});

回答by Cillier

You can supply the async: false option

您可以提供 async: false 选项

function is_alive(valueSelected) {
     result = false;
             //check if station is alive
             $.ajax({
               async: false,
               url: "lib/grab.php",
               data: "check_live=1&stream_url="+valueSelected,
               type: "GET",
                   success: function (resp) {
                     if (resp == 1) {
                       result = true;
                     } else {
                       //
                     }
                   },
                   error: function (e) {
                     console.dir(e);
                   }
             });
return result;
}