0

I'm using a click event to capture some data, query a remote server and then display the results.

The issue is the results alert shows, before the .ajax request completes. Any way to delay the alert until the results are available ?

This is what I have so far..

  $(".img").click(function(e){
        e.preventDefault();
        var data = $(this).attr("data")
        var info = data.split(','); 
        var query = '';

        $.ajax({ url : 'http://DOMAIN/' + info[5], 
            success: function(response) {
                console.log (response);
                query= response;
            } 
        }); 

        alert(info[0] + ' ' + info[1] + ' ' + query);
    });

Thanks

MacMan
  • 925
  • 1
  • 14
  • 32
  • could you try to use $.ajax events such as beforeSend:function(){} or complete:function(){} – Ranjeet Singh May 19 '16 at 09:09
  • 1
    @Frédéric Hamidi, although this question has the exact same answer as your 'duplicate' it's a completely different question... this doesn't ask how to get the reply back, it asks how the alter can be delayed 'till it does... – patrick May 19 '16 at 09:13
  • @patrick, if a question has the *exact same answer* as another, then it is a duplicate. In this specific case, it asks how to delay the alert *until the AJAX request has completed*, which is exactly the point addressed by the duplicate (`query` has to be assigned to before the call to `alert()`). – Frédéric Hamidi May 19 '16 at 09:15
  • @Frédéric Hamidi, I beg to differ... 'is it raining? yes!', 'do you want icecream? yes!' has the same answer, but still is a very different question. OP asked how he could delay the alert 'till after the AJAX call completed, your 'duplicate' was about getting data back from an AJAX call. If someone is searching how to get the alert after the AJAX call is done he we NEVER get your 'duplicate' 'cause it's not the same problem. In this case the solution is the basically the same, but it's a completely different question... – patrick May 19 '16 at 10:33
  • @patrick, your example is quite contrived, but you seem to be serious about this, so: if you want a second opinion in a private manner, you can flag this question for moderator attention and ask them to undo my closing (your flag may be declined, though). If you want a public discussion about this, you can make your case on [meta]. Fair warning: if the community there agrees with me, you will most probably get some downvotes on your meta-question. These votes won't affect your reputation, however. – Frédéric Hamidi May 19 '16 at 11:49

1 Answers1

1

Try this:

  $(".img").click(function(e){
        e.preventDefault();
        var data = $(this).attr("data")
        var info = data.split(','); 
        var query = '';

        $.ajax({ 
            url : 'http://DOMAIN/' + info[5] 
        }).done( function(){
            alert(info[0] + ' ' + info[1] + ' ' + query);
        });
    });

... or alternatively move the alert to inside your 'success' function

patrick
  • 11,519
  • 8
  • 71
  • 80