javascript - 在 href click 上发送 ajax 帖子

标签 javascript jquery joomla2.5

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

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 更改我的网址。

最佳答案

使用原生 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 解决方案使用重写等。

关于javascript - 在 href click 上发送 ajax 帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13490992/

相关文章:

javascript - 提交时更改图像源

javascript - 获取Firefox中某个标签的高度,我设置为display : table; in CSS

javascript - 当与服务器的连接丢失时,保存在 html 页面字段中输入的数据

javascript - 检查一个div是否为空

javascript - 事件绑定(bind)和修改innerHTML

jQuery 可排序仪表板对齐

javascript - 从自定义方法调用 jquery validator 的核心方法

authentication - 使用 OpenID 的 Joomla 2.5 自定义身份验证插件

css - 如果内容很少,如何让页脚保持在页面底部而不自动出现以填充空间?

php - 如何使用 php 更改 Joomla 用户组