繁体   English   中英

通过href点击发送ajax帖子

[英]Send ajax post on href click

当我单击链接时,我需要通过ajax发送post 问题是由于打开链接而导致请求被丢弃。

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

但是问题还是出在URL中(感谢joomla)-就像/~user/joomla/index.php/view-sites?view=sites&page=2如您所见,它以斜杠开头,但真正的链接是http://localhost/~user/joomla/index.php/view-sites?view=sites&page=2 ,但是在源代码中我有<a href="index.php?option=com_component&view=sites& 。'。$ option。'。 page ='。$ left。'“ // ....`

所以我需要一些“多用途域解析解决方案”,或者只是停止joomla更改我的网址。

使用native element.href将为您提供包括域的整个href,而不仅仅是属性值,还有一个范围问题,在$,post函数中使用this关键字:

$('#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;
    });
});

从您发布的链接来看,它们看起来根本不一样,因此您可能需要弄清楚一些事情,因为某些CMS解决方案使用重写等。

这个问题

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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