从ajax返回值的javascript函数

时间:2014-09-15 11:45:39

标签: javascript jquery ajax

你能解决我的问题吗?我想创建一个函数,将返回不同spicialization的医生列表。这是我的代码问题是这个代码函数doctorlist返回emtpy值。我的代码有什么问题。 你可以帮我解决我的问题吗?

$(document).ready(function () {

   $("#loaders").append("<center><img src='images/ajax-loader.gif'></center>");
   doctorSpecialty();

});

function doctorSpecialty(){

   $.ajax({
      url: "http://localhost:8080/m-help/apps/json_special.php",
      dataType: 'json',
      async: true,
      success: function(data) {
         $("#list").empty();
         $.each(data.result, function(){ 
            var specialty = doctorList(this['specialization']);

             $("#list").append("<li><a href='doctor-special.html?special=" +this['specialization']+ "' style='padding-left:10px'>" +this['specialization']+ ""+specialty+"</a></li>");
             $("#loaders").fadeOut("fast");
          });
        }
   });
}

function doctorList(e){

   var specials = e;
   var retSpecial = "";
   $.ajax({
        url: "http://localhost:8080/m-help/apps/json_special-doctor.php?s="+specials,
        dataType: 'json',
        async: true,
        success: function(data) {
           $.each(data.result, function(){ 
            retSpecial = this['docFname'];  
           });
        }
    });
    return retSpecial;
}

任何人都可以帮我修复此代码。

1 个答案:

答案 0 :(得分:0)

第二个调用不能是async = true,因为循环继续并使用了答案(尚未到达)。

您应该进行第二次异步:false

OR

在第二次调用中使用promises,它将填充专门化,你需要在第一次ajax调用中取消DOM操作。