Search code examples
phpjquerytwitter-bootstrapdrupal-7

500 (Service unavailable (with message))


I trying to call db_update() after sending jquery post, so I need to include drupal_bootstrap to use Drupal functions, here is my code :

MyModuleAPI.php

if (!defined('DRUPAL_ROOT')) {
    define('DRUPAL_ROOT', $_SERVER['DOCUMENT_ROOT']. 'mysite');
    chdir(DRUPAL_ROOT);
}
include_once(DRUPAL_ROOT.'/includes/bootstrap.inc');
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

function set_server($host,$port){

        db_update('server')...
                    ->execute();
     ...
}

$query = isset($_POST['query']) ? $_POST['query']: null;
$params = isset($_POST['params']) ? $_POST['params']: null;

switch ($query)
{   case "set_server" :
       ...
        set_server($host,$port);
        break;

}

MyModuleAjax.js

function ajax_set_server(serverHost,serverport){
    var pathUrl="path/to/includes/MyModuleAPI.php";

        jQuery.ajax({
            type: "POST",
            url: pathUrl,
            data: {query: "set_server", params:[serverHost,serverport]},
            success: function (response) {                
                console.log("set server resp "+response);
                return false;
            }
        });
}

My problem is when sending the request I get:

POST http://localhost/path/to/includes/MyModuleAPI.php 500 (Service unavailable (with message))

I don't know if the problem is from including bootstrap or if it is already legal there. Any help please.


Solution

  • In my experience, getting a 500 error when trying to send data to a php file means that there's something wrong with the php code in that file.

    While I cannot be sure what's the error might be I sure can show you how to see the php error by using developers tools. Select the network tab in order to see all the requests (your ajax request should appear among them, with the target php file name). Now select that request and under response preview you should see the php error that causing the mentioned above 500 error.

    After you'd fix that error, your request should work (200 code - success).