【问题标题】:How to get the url for a ajax page?如何获取ajax页面的url?
【发布时间】:2010-09-10 00:45:08
【问题描述】:

我对 ajax 还很陌生,我成功地将它应用到了 Drupal 站点。但我想知道,如何获取通过 ajax 加载部分内容的页面的 URL。这甚至可能吗? JSON 对象是在点击时检索的,那么在检索某个 URL 时如何使其工作? 我意识到这是一个非常广泛的问题,任何帮助将不胜感激。 提前致谢!

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

【问题讨论】:

    标签: php javascript jquery ajax drupal


    【解决方案1】:

    在页面开始时基于url page#id的hash部分加载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,但我没有测试。

    【讨论】:

    • 记得在你的模块中添加菜单处理程序。
    【解决方案2】:

    我不知道你到底想做什么。但除非你有一个创建 JSON 数据的模块,否则你需要自己做。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-31
      相关资源
      最近更新 更多