【问题标题】:How do I send an ajax post request to a specific module in Drupal6?如何向 Drupal 6 中的特定模块发送 ajax 发布请求?
【发布时间】:2011-11-17 17:25:25
【问题描述】:

我想向 Drupal6 中名为 sampleTest 的模块发送一个 ajax 发布请求。 我尝试了一些代码,但我不知道如何将模块 url 放入 jquery ajax 函数中。

<script type="text/javascript">
$(function(){
    $("#one").click(function(){
        $.ajax({ 
            type: 'POST', 
            url: 'http://localhost/drupal/www/admin/build/modules/module/get/contact', 
            dataType: 'json', 
            data: "test",
            success:function(data) {
                alert("123");
            },
            complete: function(data){
                alert("complete");  
            } 
        });
    });
});
</script>

【问题讨论】:

  • 这个问题应该移到drupal.stackexchange.com

标签: javascript ajax drupal drupal-6


【解决方案1】:

您不能像这样向模块发送 AJAX 请求,而是在模块中实现路径(使用 hook_menu()),为输出 AJAX 响应的路径提供页面回调,然后调用该路径在您的 AJAX 调用中。这是一个非常基本的例子:

function mymodule_menu() {
  $items['ajax-test'] = array(
    'access callback' => TRUE,
    'page callback' => 'mymodule_ajax_callback',
    'type' => MENU_CALLBACK
  );
}

function mymodule_ajax_callback() {
  $post_data = $_POST;

  // Do something with the post data

  $output = array('status' => 'OK');

  // For Drupal 6
  drupal_json($output);
  exit();

  // Or for Drupal 7, just in case you want to know
  drupal_json_output($output);
  drupal_exit();
}

那么你的 JS 应该是这样的:

$(function(){
  $("#one").click(function(){
    $.ajax({ 
      type: 'POST', 
      url: '/drupal/ajax-test', 
      dataType: 'json', 
      data: "test",
      success:function(data) { 
        alert(data.status); 
      },
      complete: function(data){
        alert("complete")  
      }
    });
  });
});

【讨论】:

  • 这个答案绝对值得标记为正确答案!谢谢!
猜你喜欢
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 1970-01-01
  • 2013-12-13
  • 1970-01-01
相关资源
最近更新 更多