Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 571 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drupal create a page that outputs JSON

#1
I would like to ask can one create a page that outputs JSON data as a response to Jquery Ajax request?

In a non-drupal way, I would just create a php file, for example mypage.php and then I would use

[To see links please register here]

as the URL for my AJAX request. This page will then output JSON data using json_encode().

How can I do it the Drupal way?
Reply

#2
The <a href="http://drupal.org/project/json_server">JSON server module</a> gives you JSON output of nodes.

If you want more custom JSON, you can use <a href="http://api.drupal.org/api/function/hook_menu/6">hook_menu()</a> to create a new menu callback (basically a URL path pointed to a function) and then use:

- [drupal_json()][1] - Drupal6
- [drupal_json_output()][2] - Drupal7

within that callback to send the output as JSON rather than the default HTML.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#3
A working example of scott reynen's hint: in drupal 7,
in a module called mymodule, write

function mymodule_menu() {
$items['fancystuff/json'] = array(
'access callback' => true, // available to all
'page callback' => 'mymodule_fancystuff_object', // defined below
'delivery callback' => 'drupal_json_output'
);
return $items;
}



function mymodule_fancystuff_object() {
return array('test'=>true,'dummy'=>array(0,1));
}

clear your caches, goto

[To see links please register here]

and behold
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through