user1519235
user1519235

Reputation: 73

Send AJAX on click, but still go to URL of href

I am attempting the following:

When a user clicks a 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 the href="<url>" of the link.

I tried jQuery without return false; but then the ajax doesn't work.

I tried with return false; but then the page obviously doesn't redirect to the url as I wanted.

Thanks in advance for your help.

Upvotes: 5

Views: 11204

Answers (3)

Steeven
Steeven

Reputation: 4200

Put the redirect code in the success callback:

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

Upvotes: 0

gen_Eric
gen_Eric

Reputation: 227200

Do your AJAX call, then set document.location when 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
        }
    });
});

Upvotes: 11

quickshiftin
quickshiftin

Reputation: 69581

You need to serialize these actions yourself. In the event handler first run your AJAX request, then lookup the href from the tag and use a location redirect to take the client to that URL.

Upvotes: 0

Related Questions