如何使ajax同步?

时间:2013-10-10 17:00:01

标签: javascript jquery ajax

如何使用此功能?

         //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);
               }
         });

我以为我可以这样做:

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;
}

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

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

似乎工作:

        //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();
          });
        }

并称之为:

    //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);
            //
          }
      });
    });

2 个答案:

答案 0 :(得分:7)

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

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); 
});

答案 1 :(得分:5)

您可以提供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;
}