1

I'm trying to run a function before async ajax request. However function is running after async request gets the respond.

Is there any way to solve this issue?

block();

ajaxRequest= $.ajax({
    url: siteURL+'includes/ajax/action.php',
    type: "POST",
    async: false,
    data: {productID : productID},
    dataType: "json"
});
ajaxRequest.done(function(data) {
            block(true);
            if (data === false) {
                alerts('error title','error info here', 'error', 200);
                return false;
            }
});
ajaxRequest.fail(function(jqXHR, textStatus) {block(true); alerts('error title','error info','error');});
confirm();

I run more functions after these codes. However as I stated before, block(); function is waiting till async ajax request is getting response.

If I don't run asynchronous, then I get block() and confirm() functions running at the same time so return false; losing all the meaning.

P.S. I run these codes when a form is submitted so if async request is failed I don't want it to run any other code after it. However when it is asynchronously running block() is waiting till response is returned.

7
  • Call the function before you call the ajax request by putting the line that calls the function above the line that executes the ajax request. Commented Oct 12, 2012 at 19:47
  • well maybe that wasn't obvious but I already run the function before. Commented Oct 12, 2012 at 19:48
  • Well, you didnt' post any code, so it's just guessing. ;-) Commented Oct 12, 2012 at 19:48
  • Can you please explain what you're trying to accomplish? The original block() is running before the request is sent. Commented Oct 12, 2012 at 20:03
  • @Snuffleupagus If block() is performing a graphical or dom change, it may not appear to happen before the ajax request due to async:false stopping browser rendering. Commented Oct 12, 2012 at 20:06

1 Answer 1

4

Your problem is async:false. Your request is NOT an async request, it is a sync request. It blocks javascript code and browser rendering from happening while the request is being processed. With your current setup, there should be no harm in removing async: false, thus making it async.

block();

ajaxRequest= $.ajax({
    url: siteURL+'includes/ajax/action.php',
    type: "POST",
    data: {productID : productID},
    dataType: "json"
});
ajaxRequest.done(function(data) {
            block(true);
            if (data === false) {
                alerts('error title','error info here', 'error', 200);
                return false;
            }
});
ajaxRequest.fail(function(jqXHR, textStatus) {block(true); alerts('error title','error info','error');});
ajaxRequest.always(confirm);
Sign up to request clarification or add additional context in comments.

4 Comments

@Revenant This solution should work perfectly. If for some unknown reason, it doesn't you may want to consider using the 'beforeSend' callback built into the jQuery .ajax functionality.
beforeSend might be really helpful. However it will push me to add over 300 lines of codes to in beforeSend. The same goes to confirm() and codes after that. I believe there is no easy way around this. Maybe I should say; I just want block() to run after that wait for a response from ajax request and then proceed (if ajax request is not returning false).
@Revenant did you see the sample code i just added? it makes confirm run after the async request.
Yes Kevin B, thank you for your help. It will just require massive editing to my .js file. I just wanted to make things short by adding block(); and confirm();. However there are hundreds of lines of codes before and after that ajax request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.