0

I am getting some problems calling a custom module function from jQuery and AJAX.

ajax_example.module

function ajax_example_init() {
    drupal_add_js(drupal_get_path('module', 'ajax_example') . '/ajax_example.js', array('scope' => 'footer'));
}

function ajax_example_menu() {
  $items['ajax_username'] = array(
    'title' => 'Get Current User Name',
    'page callback' => 'ajax_example_get_username',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function ajax_example_get_username() {
  global $user;
  $user_name = $user->name;
  drupal_json_output($user_name);
}

ajax_example.js

(function ($) {
  Drupal.behaviors.ajax_example = {
    attach: function (context) {
      $.ajax({
        url: '/ajax/username',
        success: function(data) {
          console.log(data);
        }
      });
    }
  }
})(jQuery);

I am trying to call JavaScript by a JS Injector module using these commands.

(function($) {
  $.ajax({
    url: 'ajax_username',
    type: "GET",
    success: function (data) {
      console.log(data); 
    } 
  });
})(jQuery);

Unfortunately, I get the same 404 error again and again.

What am I doing wrong?

  • Welcome to Drupal Answers! Why is ajax_example.js using /ajax/username as URL, and the other code uses ajax_username as URL? From the implementation of hook_menu(), the URL should be /ajax_username, not the ones you are using. – kiamlaluno Dec 27 '17 at 21:25
  • Hi @kiamlaluno, I appreciate your help. I have modified as suggested, but the call from the js injector still fails (the 3rd block of code I sent). I have created a module with three files: - ajax_example.module - ajax_example.js - ajax_example.info If I activate the module, ajax_example.js works and the information is displayed in the browser console. However, my main goal is to call the PHP function using ajax and jquery (the 3rd block of code I sent). Unfortunately, this call does not work. Any clue? – arturocu81 Dec 28 '17 at 20:59
  • I still have to understand why you are showing two different JavaScript snippets, and what you call JS injector. – kiamlaluno Dec 28 '17 at 21:29
  • @kiamlaluno ajax_example.js is the JS file integrated with the module that I created. The second block of JS code is a test I am trying to do to call the ajax_username function of ajax_example.module. Why? I do not want to activate the module. I would like to use the module functionality just on one specific page. – arturocu81 Dec 28 '17 at 22:31
1

To answer to The second block of JS code is a test I am trying to do to call the ajax_username function of ajax_example.module. you wrote in your comment, what you can use in JavaScript code like $.ajax({url: 'ajax_username'}) as url is a route exposed in the hook_menu() implementation of a module, or the path of a PHP file. It isn't the name of a PHP function implemented from a module, nor the name of a file containing Javascript code (without the .js extension).

As concrete example of what Drupal core modules do, see the content of the statistics.js file.

(function ($) {
  $(document).ready(function() {
    $.ajax({
      type: "POST",
      cache: false,
      url: Drupal.settings.statistics.url,
      data: Drupal.settings.statistics.data
    });
  });
})(jQuery);

What $.ajax() gets as url is Drupal.settings.statistics.url, set from statistics_node_view() with the following code.

$settings = array('data' => array('nid' => $node->nid), 'url' => url(drupal_get_path('module', 'statistics') . '/statistics.php'));

In the test site I am using, the output of url(drupal_get_path('module', 'statistics') . '/statistics.php') is /modules/statistics/statistics.php.

So, if you want to call the callback associated to your ajax_username menu, you need to use $.ajax({url: '/ajax_username'}) or $.ajax({url: '/index.php?q=ajax_username'}), but in both the cases, the module must be installed and enabled.

If you don't want to enable a module for that, you cannot use hook_menu(), but you could create a PHP file containing the code to use, in the same way the Statistics module does with its statistics.php file.
Be warned: Use this only if you know exactly what you are doing, as using the wrong code could be a security risk for your site and its data.

I would rather use hook_menu().

0

Your first JS example has a URL of /ajax/username, which according to the hook_menu implementation you haven't registered, so that explains the 404 for that one.

The second JS example uses an absolute rather than relative URL, which will only work if you're on the first level of the path (/, /foo, /bar, etc).

Assuming your module is installed and enabled, is definitely named ajax_example, and Drupal's caches have been cleared, it seems most likely that you're executing that second example on a path that's lower than the root (e.g. /foo/bar), and the fact that the URL is absolute means the browser is looking for /foo/bar/ajax_username, which doesn't exist.

Changing the URL to /ajax_username in JS should fix the problem.

  • Hi @clive, I appreciate your help. I did the modifications, but the call from the js injector still fails (the 3rd block of code I sent). I created a module with this files: ajax_example.module ajax_example.js ajax_example.info If I activate the module, it works (the information is displayed in the browser console). But my goal is to call the PHP function using ajax and jquery by the JS injector module (the 3rd block of code I sent) without activating the module. I'm pretending to call a certain PHP function from a specific page. Unfortunately, this call does not work. Any clue? – arturocu81 Dec 28 '17 at 21:07

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.