1

Hi I am developing a module in which i am sending data from javascript to drupal using jquery ajax.

but i am getting 404 error undefined.I am not able to figure out. Pls help

Here is my code

This is tbg.js an external js file

$.ajax({
        url: 'localhost/drupal/my_currenttime/10',
        type: 'GET',
        success: function(data){
            alert(data);
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert(jqXHR.status+textStatus + errorThrown);
        }
    }); 

In my module file i am calling this js file in hook_nodeapi

function modulename_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) 
{
  switch($op)
  {
    case 'view':
    drupal_add_js(drupal_get_path('module', 'modulename').'/tbgscript.js', 'module');
    break;
  }  
}

I have created a call back url for the ajax request in modulename_menu() hook

function modulename_menu() 
{
  $items['my_currenttime/%'] = array(
  '#title' => 'My custom callback',
  'description' => 'Desc',
  'page callback' => 'custom_currentTime',
  'page arguments' => array(1),
  'access arguments' => array('access content'),
  'type' => MENU_CALLBACK,
  );

  return $items;
}

and my page call back method is

function custom_currentTime($args)
{
   echo "HELLO".$args;
}

i want to access the value $args in my module file.

  • I'm not an expert on modules and ajax, but - isn't the 'access callback' key missing from modulename_menu? – Artur May 26 '12 at 10:18
  • I think its not required. it will take default value "user_access" – yashbinani May 26 '12 at 10:24
1

I would guess that the URL is wrong in your javascript.

Currently, if your site is on http://localhost, the page requested will be:

http://localhost/localhost/drupal/my_currenttime/10

When I suspect it should be:

http://localhost/drupal/my_currenttime/10

To fix that just add the http:// part to the beginning of the URL variable in your JS code, or make it relative:

url: '/drupal/my_currenttime/10'

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.