2

I am trying to send an array back to Drupal to save in a database table. I'm getting an error 400 though when inspecting in the console and the url seems correct:

GET http://localhost/audiophreak/callback/%?[1,2,3,4,5,6,45,46,47] 400 (Bad Request)

Here is my hook_menu:

/**
* Implementation of hook_menu().
*/
function my_db_module_menu() {
  $items['callback/%?'] = array(
    'title' => 'My callback',
    'description' => 'Desc',
    'page callback' => 'mymodule_getinfo',
    'type' => MENU_CALLBACK,
    'access arguments' => array('access content'),
    'page arguments' => array(1),
  );
  return $items;
}

Here is my callback:

/**
 * My callback
 */
function mymodule_getinfo($data) {

echo drupal_json_decode($data);


  // //Parse the data and input it into the database
  // $result = db_insert('my_db')
  // ->fields(array(
    // 'nid' => $data->node,
    // 'uid' => $data->user,
    // 'progress' => $data->progress,
  // ))
  // ->execute();
}

And here is my AJAX

//Data
    var post = plnzVideoTracker.progress


    //Begin Ajax
    $.ajax({
          url: '/audiophreak/callback/%',
          contentType: "application/json; charset=utf-8",
          type: 'GET',
          dataType: 'json',
          data: JSON.stringify(post),
          success: function(data) {
            if(data == "success")
            {
            $('.status').attr("innerHTML","<font color='green'>Name submitted successfully!</font>");
            }
            else
            {
              $('.status').attr("innerHTML","<font color='red'>Name submit failed!</font>");
            }
            },
            beforeSend: function() {
              $(document).ready(function () {
              $('.status').attr("innerHTML","Loading....");
            });
            },
            'error': function(data) {
              $(document).ready(function () {
              $('.status').attr("innerHTML","ERROR OCCURRED!");
            });
          }
     });

plnzVideoTracker.progress is an array inside of an object literal if that has anything to do with it?

1

The % symbol is type of placeholder in hook_menu item. You can read more about it here: https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_menu/7#sec_path_wildcards.

For you example I suggest to use the following:

function my_db_module_menu() {
  $items['callback'] = array(
    'title' => 'My callback',
    'description' => 'Desc',
    'page callback' => 'mymodule_getinfo',
    'type' => MENU_CALLBACK,
    'access arguments' => array('access content'),
  );

  return $items;
}

Simple path - callback.

And use the following AJAX:

$.ajax({
  url: '/callback',
  type: 'POST',
  dataType: 'json',
  data: {data: post},
  ...

By this way, you can access the data in your webhook more simply:

/**
 * My callback
 */
function mymodule_getinfo() {
  $data = !empty($_POST['data']) ? $_POST['data'] : array();

  // do what you want...
}
5
  • Hmm now getting the error: POST localhost/callback 404 (Not Found). Url seems to be deprecating the base path – Michael W Sep 22 '15 at 1:55
  • Did you cleared the cache before try to access it? – kala4ek Sep 22 '15 at 1:56
  • Yes, i'll double check everything again though. uno momento. – Michael W Sep 22 '15 at 1:58
  • kala4ek, I seem to have solved it partially thanks to you. I set the callback in the ajax request to a relative path: $.ajax({ url: '/audiophreak/callback', type: 'POST', dataType: 'json', data: {data: post}, success: function(response){ alert(response); //echo what the server sent back... } }); and used if(isset($_POST['data']) && !empty($_POST['data'])) { // echo json_encode(array("blablabla"=>$variable)); echo json_encode($_POST['data']); } – Michael W Sep 22 '15 at 2:13
  • This now alerts the array I am sending. Thank you! – Michael W Sep 22 '15 at 2:14

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.