如何获取ajax页面的网址?

问题描述:

我对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'); 
} 

我不知道究竟你正在尝试做的。但是除非你有一个创建JSON数据的模块,否则你需要自己去做。

您需要使用hook_menu创建菜单回调。在回调函数中,您可以使用drupal_json返回json数据。

如果您自己创建菜单回调等,该URL将是您所做的任何事情。否则,您只需使用您要使用的模块定义的URL。

基于链接页面#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