简体   繁体   中英

jquery .ajax's .done function get done on alert only

This seems a bit strange and i cant get a hold of the situation, apparently i used jquery's .ajax() function to process some script to get some data from the database. The script works fine and data gets returned as accordingly:

$.ajax({
    type: "POST",
    url: "getevaluationdata.php",
    data: { evaluation_ID: value }
})
.done(function( msg ) { 
    $("#error2").html(msg);
});

After the scripts process, it is suppose to populate the data echoed in the script to the div i mentioned, but this does not happen. However, if i write an alert statement before the div population, the div gets populated. meaning:

$.ajax({
    type: "POST",
    url: "getevaluationdata.php",
    data: { evaluation_ID: value }
})
.done(function( msg ) {
    alert(msg);
    $("#error2").html(msg);        
});

I dont seem to get why this is happening and what I can do to resolve this. PS I have done this before, and this was working correctly before

Is it possible #error2 is not on the page yet?, so the alert gives it time? try putting the ajax call in

$(function(){

    $.ajax({
      type: "POST",
      url: "getevaluationdata.php",
      data: { evaluation_ID: value }
    }).done(function( msg ){
      $("#error2").html(msg);
    });
});

Another thing you can try for reference is using the success callback

$.post("getevaluationdata.php",{evaluation_ID:value},function(msg){
    $("#error2").html(msg);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM