0

Background

I have a Drupal 7 module that is making an AJAX call via jQuery.ajax() to a menu callback that I've defined in my module. The purpose isn't to return markup, but rather just a single string value.

This is all working as it should, but the method by which I'm passing the value back to jQuery seems messy to me.

I'm using:

// $condition is a simple string value.
print($condition);
drupal_exit();

My Question

Is there a cleaner, more standardized way of doing this? This just feels hackish and messy to me.

Thanks, Drupalers!

0

That will work, but here is how I would do it (in D7). You can set up the ajax callback server side like this:

$response = array('value' => $condition);
drupal_json_output($response);
drupal_exit();

Then your value will be available as data.value in your success callback:

$.ajax({
  url:example.com/some/path
  success:function(data) {
    alert(data.value);
  }
});
  • Thanks, Jaypan. That does look a little cleaner. Is it normal to finish off an AJAX callback with drupal_exit()? – Beau May 2 '17 at 13:31

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.