1

Passing multiple array to drupal_json_output() is not giving correct output in jquery ajax.

I am using a menu_callback function and returning a multiple array like

function test_callback() {
   for($i = 0; $i < 10; $i++) {
    $arr = array('message' => 'test'.$i, 'data' => 'data'.$i);
    $arrs[] = $arr;
   }
 drupal_json_output($arrs);
}

Now, in the jquery, I wrote like this,

$.ajax({
url: the above url,
dataType: 'json',
success: function(data) {
$(data).each(function(key,val) {
  console.log(val.message);
});
})
});

The output in the console is giving an object.

I tried to use $.each for the val also, but the output it is returning as undefined. I tried in a lot of ways using $.get etc. but not getting the output. Can anyone help me here??

3
  • The return of the array in json format should be correct. When calling your service URL directly in browser, do you have an output only containing the array or also some HTML code from the theme (doctype, <head>, ...)? Oct 19, 2013 at 9:03
  • No, I am returning only the array. Does not contain any HTML code
    – Kiran Tej
    Oct 19, 2013 at 9:16
  • Could you dump out a portion of the returned JS object into the question? I can't see any reason it wouldn't be coming back as an array, it would interesting to see the exact format you get
    – Clive
    Oct 19, 2013 at 12:37

2 Answers 2

5

To setup a service which returns a json formatted data, you should set the delivery callback to drupal_json_output. This will only output the service data and not all the HTML defined in your theme.

For example

function YOURMODULE_menu()
{
    $items = array();

    $items['service/ajax'] = array(
        'title' => t('Ajax service'),
        'page callback' => 'YOURMODULE_ajax',
        'delivery callback' => 'drupal_json_output',
        'access arguments' => array('access content'),
        'type' => MENU_CALLBACK,
    );

    return $items;
}

function YOURMODULE_ajax()
{
    for($i = 0; $i < 10; $i++) {
        $arr = array('message' => 'test'.$i, 'data' => 'data'.$i);
        $arrs[] = $arr;
    }

    return $arrs;
}
5
  • Ok. I will try this.
    – Kiran Tej
    Oct 19, 2013 at 9:22
  • Ok, let me know if it works. Oct 19, 2013 at 11:30
  • Wouldn't the delivery callback be ajax_deliver? I've never seen drupal_json_output used for that purpose before. Core and contrib use ajax_deliver
    – Clive
    Oct 19, 2013 at 11:55
  • @MikeVranckx: I have disabled php_json in previously. Now, after checking the settings, I have enabled the json extension and it worked like a charm without using the delivery callback. Anyway thanks for your post.
    – Kiran Tej
    Oct 19, 2013 at 13:22
  • @KiranTej Ok good and you're welcome. @Clive I'm using drupal_json_output to return a custom json response with correct response header. ajax_deliver would also return as json but with extra data / commands added by Drupal. Oct 19, 2013 at 13:28
0

I guess we need to let the ajax call know that the response is in JSON, once give a try to the following code:

// Callback function of ajax (PHP file).
function test_callback() {
  for($i = 0; $i < 10; $i++) {
    $arr = array('message' => 'test'.$i, 'data' => 'data'.$i);
    $arrs[] = $arr;
  }

  // Returns data in JSON format.
  drupal_json_output($arrs);
}

// Js code for making an ajax call, and handling the response of ajax.
$.ajax({
  url: the above url,
  dataType: 'json',
  success: function(data) {
  // Check the reponse data in the firebug console.
  console.log(data);

  /** Commenting the code for now, once you get the data than uncomment this.
  $(data).each(function(key,val) {
    console.log(val.message);
  });
  */
}, 'json');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.