Skip to content

Instantly share code, notes, and snippets.

@keopx
Created September 24, 2015 12:39
Show Gist options
  • Select an option

  • Save keopx/3c53e278f38398d133d4 to your computer and use it in GitHub Desktop.

Select an option

Save keopx/3c53e278f38398d133d4 to your computer and use it in GitHub Desktop.
Drupal ajax call with values
function get_data_info() {
jQuery.ajax({
url: 'mymodule/api/data',
type: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
dataType: "json",
data: {
'data': data
},
success: function (result) {
// custom code
},
error: function (error, result) {
// Custom code.
}
});
}
function mymodule_menu() {
$items = array();
//...
$items['mymodule/api/data'] = array(
'page callback' => 'service_ajax',
'access arguments' => array('access content'),
);
//...
return $items;
}
function service_ajax() {
$data = check_plain($_POST['data']);
if (empty($data) {
$return = 'false';
} else{
$return = 'true';
}
drupal_json_output($result);
drupal_exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment