Send AJAX on click but still go to href url

I am trying to do the following:

When the user clicks the link, I want to update a specific column of a specific table in the database via ajax. BUT I still want the user to be redirected to href="<url>"links.

I tried jQuery without return false;, but then ajax does not work.

I tried with return false;, but then obviously the page does not redirect the url as I wanted.

Thanks in advance for your help.

+5
source share
3 answers

Make your AJAX call, then set document.locationwhen done.

$('a').click(function(e){
    var href = this.href;  // get href from link
    e.preventDefault();  // don't follow the link
    $.ajax({
        url: '/path/to/site',
        data: {some: data},
        success: function(){
            document.location = href;  // redirect browser to link
        }
    });
});
+11
source

. , AJAX, href , URL.

0

Put the redirect code in the success callback:

$.ajax({
  ...
  success: function(){
    $(location).attr('href',url); // You URL inserted
  }
});
0
source

All Articles