簡體   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