当我点击链接时,我需要发送 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/