0

I want on onclick event send some data to module and do some processing and get result back.

I have module name 'custom' and js code i have mentioned below. Url of page from where ajax call is made is 127.0.0.1/field-work

    Drupal.behaviors.custom = function (context, settings)
 {
    $(document).on('click', '#clear-filter', function() { 
    console.log("Hello");
    jQuery.ajax({
            type: 'POST',
            url: 'custom/field-work',
            dataType: 'json',
            success: function(data){
            console.log(data);
           },
        });
    });
};

Module code to receive ajax call have mentioned below.

function custom_menu() {
    $items['custom/field-work'] = array(
    'title' => t('foo AJAX'),
    'type' => MENU_CALLBACK,
    'page callback' => 'details_data',
    'access arguments' => array('access content'),
    );
    return $items;
}

From details_data i am returning some random data. But ajax call is not working. If i am removing drupal behaviour the js part is working fine.

Please help me out.

0

Please reread the documentation to drupal ajax documentation, particularly how Drupal behaviours do work.

You are missing one crucial part. Drupal calls the attach-function of your behaviour on documentReady or before DOM-elements get injected into the current document. As your JS-code does not have an attach-method, nothing happens. The code below should give you an idea how to fix your problem:

Drupal.behaviors.custom = function (context, settings) 
{
  attach: function(context, settings) {
    $(document).on('click', '#clear-filter', function() { 
      console.log("Hello");
      jQuery.ajax({
        type: 'POST',
        url: 'custom/field-work',
        dataType: 'json',
        success: function(data){
          console.log(data);
        },
      });
    });
  }
};
  • 1
    Forget to add (function($) { .... }) (jQuery); – Jimmy Ko Aug 8 '16 at 9:12
  • thanks @stephan . Ajax call is being made using behavior. But in module i have defined below code . But this function is not being called. I am getting in response current page html. In request data is being sent. function custom_menu() { $items['field-work'] = array( 'title' => t('foo AJAX'), 'type' => MENU_CALLBACK, 'page callback' => 'details_data', 'access arguments' => array('access content'), ); return $items; } – krishnaisdinesh Aug 8 '16 at 9:39
  • Details data function : function details_data(){ drupal_json($_POST); exit(); } – krishnaisdinesh Aug 8 '16 at 9:41
0

Drupal Behavior work with attach function. So you need to call as

Drupal.behaviors.custom = { 
  attach: function (context, settings)
    {
        $(document).on('click', '#clear-filter', function() { 
        console.log("Hello");
        jQuery.ajax({
                type: 'POST',
                url: 'custom/field-work',
                dataType: 'json',
                success: function(data){
                console.log(data);
               },
            });
        });
  }
};

For additional reference click

  • thanks @mahtab. Ajax call is being made using behavior. But in module i have defined below code . But this function is not being called. I am getting in response current page html.Post data is sent. function custom_menu() { $items['field-work'] = array( 'title' => t('foo AJAX'), 'type' => MENU_CALLBACK, 'page callback' => 'details_data', 'access arguments' => array('access content'), ); return $items; } I am not getting response. Details function: function details_data(){ drupal_json($_POST); exit(); } I tried to track jquery error part is being executed with status 200. – krishnaisdinesh Aug 8 '16 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.