0

I created a custom path via hook_menu with a page callback to a function that prints json.

It all works in the sense that requesting: http://cms.tripchi.com/tripchi/getme returns my JSON, but the header says it's 404 which is throwing off everything in the app. This only returns the 404 status on the client's godaddy server (PHP Version 5.4.19 w/ CGI/FastCGI), but returns 200/Ok on my local PHP Version 5.3.5 w/ Apache 2.0 Handler. All Drupal configurations and databases are equal.

hook_menu

function tripchi_menu() {
    return array(
        'tripchi/getme' => array(
            'title' => 'My Profile',
            'description' => 'mobile test for user status and object',
            'access callback' => 'user_access',
            'access arguments' => array('access content'),
            'page callback' => 'tripchi_getme')
    );
}

callback function

function tripchi_getme(){
    $user = $GLOBALS['user'];
    $json = array();
    // ... a bunch of stuff to construct a special array;
    drupal_add_http_header('Status', 'Ok');
    drupal_add_http_header('Access-Control-Allow-Origin', '*');
    http_response_code(200);
    //echo json_encode($json);
    drupal_json_output($json);
    drupal_exit();
    //die();
    // i've tried all combinations of commenting/uncomming the above...
}

below screenshot of the requests locally (right) and remotely (left): below screenshot of the requests locally (right) and remotely (left)

why does this always return 404 but also give me my perfectly json encoded data?

  • i rebuilt the whole Block into a Service and i'm getting my data with a 200/Ok code. the link above will now show 200, but still have not figured out what was wrong but right now i'm blaming the Apache/PHP configure over Drupal. – E.A.T Aug 20 '14 at 11:02
1

First of all, for JSON output use drupal_json_output as a delivery callback in your menu. It will make thinks much easier:

function tripchi_menu() {
    return array(
        'tripchi/getme' => array(
            'title' => 'My Profile',
            'description' => 'mobile test for user status and object',
            'access callback' => 'user_access',
            'access arguments' => array('access content'),
            'page callback' => 'tripchi_getme',
            'delivery callback' => 'drupal_json_output'
         ),
    );
}

Then in your menu callback just return array. You don't need any other fancy stuff. I also faced 404 issue, but with this solution AND explicitly set response code 200 everything worked fine.

function tripchi_getme(){
    global $user;
    $json = array();
    // ... a bunch of stuff to construct a special array;

    // Only this was needed to make the stuff work.
    http_response_code(200);

    return $json;
}

I couldn't tell if the cause of my trouble was drupal or apache or both, but this certainly helped. Note that Access-Control-Allow-Origin was not needed to make this work (and I didn't plan to use cross domain calls either).

| improve this answer | |
  • i was really hoping this was the answer but it did not make a difference for me. I tried the 'delivery callback' with all varieties of returning $json and drupal_json_output($json) with and without explicit headers like http_resposne_code(200). All return a 404 response code, but either proper json or html with the error Fatal error: Cannot use object of type stdClass as array in /home/xxxx/includes/common.inc on line 5836 – E.A.T Aug 31 '14 at 8:38
  • @EliATaylor The error you quoted suggests that your menu callback was returning an object instead of array. – SiliconMind Aug 31 '14 at 15:34
  • i just double-backed on this and found this answer was correct. Thanks! – E.A.T Feb 16 '15 at 22:00
0

Tried code below. Works fine. Did you check if the user you're using has 'access content' permission?

... hook_menu ...
  $items['tripchi/getme'] = array(
    'title' => 'My Profile',
    'access arguments' => array('access content'),
    'page callback' => 'tripchi_getme',
  );

return $items; }

function tripchi_getme(){ $json = array(); // ... a bunch of stuff to construct a special array; drupal_add_http_header('Access-Control-Allow-Origin', '*'); drupal_json_output($json); }

enter image description here enter image description here

| improve this answer | |
  • thanks @rostislav but yes, i've tried with and without the permissions. i've updated my question with a similar photo of requests from both my local and remote machine. plus shouldn't a permissions issue cause a different code and not return my data? – E.A.T Aug 19 '14 at 19:15
0

for the record, i ended up giving up on Blocks and moved this whole function under Services which already replies in JSON and seems to handle access / reponse codes properly.

| improve this answer | |

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.