简体   繁体   中英

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.

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.

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;
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM