2010-09-07 130 views
0

我對ajax非常陌生,並將它成功應用於Drupal站點。但我想知道,我怎樣才能得到一個頁面的URL,其中一部分內容是通過ajax加載的。這甚至有可能嗎? JSON對象是通過點擊來檢索的,那麼如何在檢索某個URL時使其工作? 我意識到這是一個非常廣泛的問題,任何幫助將不勝感激。 在此先感謝!如何獲取ajax頁面的網址?

我的JS是這樣的:

Drupal.behaviors.ajax_pages = function (context) { 
    $('a.categoryLink:not(.categoryLink-processed)', context).click(function() { 
    var updateProducts = function(data) { 
     // The data parameter is a JSON object. The films property is the list of films items that was returned from the server response to the ajax request. 
     if (data.films != undefined) { 
     $('.region-sidebar-second .section').hide().html(data.films).fadeIn(); 
     } 
     if (data.more != undefined) { 
     $('#content .section').hide().html(data.more).fadeIn(); 
     } 

    } 
    $.ajax({ 
     type: 'POST', 
     url: this.href, // Which url should be handle the ajax request. This is the url defined in the <a> html tag 
     success: updateProducts, // The js function that will be called upon success request 
     dataType: 'json', //define the type of data that is going to get back from the server 
     data: 'js=1' //Pass a key/value pair 
    }); 
    return false; // return false so the navigation stops here and not continue to the page in the link 
}).addClass('categoryLink-processed'); 
} 

回答

0

我不知道究竟你正在嘗試做的。但是除非你有一個創建JSON數據的模塊,否則你需要自己去做。

您需要使用hook_menu創建菜單回調。在回調函數中,您可以使用drupal_json返回json數據。

如果您自己創建菜單回調等,該URL將是您所做的任何事情。否則,您只需使用您要使用的模塊定義的URL。

1

基於鏈接頁面#ID的散列部頁面加載AJAX的開頭,

$(document).ready(function() { 
    $.ajax({ 
    type: 'POST', 
    url: "/path/to/page", 
    success: updateProducts, 
    dataType: 'json', 
    data: 'id=' + window.location.replace(/.*#/, '') 
    }); 

    $('a.categoryLink:not(.categoryLink-processed)', context).click(function() { 

    $.ajax({ 
     type: 'POST', 
     url: "/path/to/page", 
     success: updateProducts, 
     dataType: 'json', 
     data: 'id=' + this.href.replace(/.*#/, '') 
    }); 
}); 

或者你可以使用jquery-history但我沒有測試它。

+0

記得在模塊中添加菜單處理程序。 – 2010-09-09 16:55:52