Remote Ajax Call in jQuery .click() function doesn't finish before going to next page.

Posted by Clint on Stack Overflow See other posts from Stack Overflow or by Clint
Published on 2010-06-10T15:20:26Z Indexed on 2010/06/10 15:22 UTC
Read the original article Hit count: 259

I need to send click information on my website to a third party server using ajax (json). I am using jquery and I added the click event to certain links. In the click event I am making a json request to a remote server with the location of the click (heat map) and some other information. The problem is that the ajax function doesn't fire in time before the default link action happens. Setting async to false doesn't seem to work on remote ajax calls. I have tried preventDefault(), but then I don't know how to run the default action after the successful ajax call. Here is what I want to do:

$('a').click(submit_click);

function submit_click(e,fireAjax){
    e.preventDefault();
    cd_$.ajax({
        url: jsonUrl, //remote server
        dataType: 'json',
        data: jsonData,
        async: false,
        success: function(reply){
            //Run the default action here if I have to disable the default action
        },
    });
}

Any suggestions?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery