Kin
Kin

Reputation: 4596

Send ajax post on href click

When i click a link i need to send post via ajax. The problem is that the request is dropped because of open a link.

jQuery('#pagination a').click(function(){
    jQuery.post('index.php?option=com_component',
        {checked_sites_for_session: jQuery('.selected-site input[type="checkbox"]').map(function () {return this.value;}).get()}
    ).success(function(){window.location = jQuery(this).attr('href')});
    console.log(jQuery(this).attr('href'));
    return false;
});

But again the problems is (thanks to joomla) in url - its like /~user/joomla/index.php/view-sites?view=sites&page=2 - as you see it starts from slash, but the real link is http://localhost/~user/joomla/index.php/view-sites?view=sites&page=2, but in the source i have <a href="index.php?option=com_component&view=sites&.'.$option.'page='.$left.'"//....`

So i need some "multipurpose domain parsing solution" or just stop joomla changing my url's.

Upvotes: 1

Views: 1249

Answers (2)

thecodejack
thecodejack

Reputation: 13379

problem with this

    jQuery('#pagination a').click(function(e){
    e.preventDefault();
var me=this;
        jQuery.post('index.php?option=com_component',
            {checked_sites_for_session: jQuery('.selected-site input[type="checkbox"]').map(function () {return this.value;}).get()}
        ).success(function(){window.location = jQuery(this).attr('href')});
        console.log(jQuery(me).attr('href'));
        return false;
    });

Upvotes: -1

adeneo
adeneo

Reputation: 318212

Using the native element.href will get you the entire href including the domain, and not just the attributes value, also there's a scope issue with the this keyword being used inside the $,post function :

$('#pagination a').on('click', function(e){
    e.preventDefault();
    var self = this;
    $.post('index.php?option=com_component',
        {checked_sites_for_session: $('.selected-site input[type="checkbox"]').map(function () {
                 return this.value;
            }).get();
        }
    ).success(function(){
        window.location = self.href;
    });
});

From the links you've posted they don't look to be the same at all, so you might have to figure something out for that, as some CMS solutions use rewriting etc.

Upvotes: 2

Related Questions