0

Can you help with my problem. I want to make a function that will return list of doctors in different spicialization.. this is my code the problem is this code the function doctorlist returns emtpy value. what is the wrong with my code. can you help me to fixed my problem please.

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

Anyone can help me to fixed this code for please.

PPjyran
  • 95
  • 1
  • 1
  • 9
  • I would first suggest cleaning up your code a little: its somewhat redundant and inefficient to make a second async call for each result returned by the first async call. Why not have one call which returns all the required data? Also, the $.each() utility function requires a callback with parameters. You're trying to access 'this', which probably has a meaning you are not expecting... – DvS Sep 15 '14 at 12:04
  • i am new with ajax and javascript.. how can i do that can you fixed it for me. – PPjyran Sep 15 '14 at 12:13

1 Answers1

0

The second call can not be async = true, since the loop continues and uses the answer (which has not arrived yet).

You should make the second async:false

OR

Use promises in the second call which will fill in the specialization and you need to cancel the DOM manipulation in the first ajax call.

Scription
  • 646
  • 3
  • 12
  • 21
  • Please see Jquery site: http://api.jquery.com/jquery.ajax/ // Assign handlers immediately after making the request, // and remember the jqXHR object for this request var jqxhr = $.ajax( "example.php" ) .done(function() { alert( "success" ); }) .fail(function() { alert( "error" ); }) .always(function() { alert( "complete" ); }); // Perform other work here ... // Set another completion function for the request above jqxhr.always(function() { alert( "second complete" ); }); – Scription Sep 15 '14 at 11:58
  • there any ways to do that? – PPjyran Sep 15 '14 at 12:06
  • Your question has been marked as duplicate, please see the solution given to the original question and see if it helps – Scription Sep 15 '14 at 12:07