Created
November 7, 2012 11:14
-
-
Save netsensei/4030906 to your computer and use it in GitHub Desktop.
Minimal Drupal Bootstrap for AJAX requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* | |
* Minimal AJAX page. Call like this: | |
* $.ajax({ | |
* type: 'GET', | |
* url: '/drupal-ajax.php?param1=... | |
* data: {}, | |
* dataType: 'json', | |
* success: function (data) { | |
* // Do stuff | |
* }, | |
* error: function (xmlhttp) { | |
* alert(Drupal.t('An HTTP error @status occurred.', {'@status': xmlhttp.status})); | |
* } | |
* }); | |
*/ | |
define('DRUPAL_ROOT', getcwd()); | |
// Include whatever you need. Base functions reside in Drupals' /includes folder | |
// You can also include module files, but be aware of dependencies! | |
require_once DRUPAL_ROOT . '/includes/bootstrap.inc'; | |
require_once DRUPAL_ROOT . '/includes/common.inc'; | |
// .. | |
// If you want to use dd() for debugging purposes, include these too! | |
// require_once DRUPAL_ROOT . '/includes/file.inc'; | |
// require_once DRUPAL_ROOT . '/sites/all/modules/contrib/devel/devel.module'; | |
// Minimal bootstrap of Drupal | |
drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION); | |
// dd() uses stream wrappers in file_put_contents() to write drupal_debug.txt. | |
// This function registers the implementations with PHP during the call. | |
// Uncomment too! | |
// file_get_stream_wrappers(); | |
$output = ''; | |
// Do logic! | |
// Set the correct content type | |
drupal_add_http_header('Content-Type', 'text/javascript; charset=utf-8'); | |
// IE9 does a nasty client side caching. We explicitly trigger a cache miss | |
drupal_add_http_header('Cache-Control', 'no-store, no-cache, must-revalidate'); | |
drupal_add_http_header('Pragma', 'no-cache'); | |
// Return your output as a JSON object for easy JS integration | |
print drupal_json_encode(array( | |
'content' => $output, | |
)); | |
exit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does not work for me, I always get an empty string back from it despite the status being 200 (success).