Created
September 24, 2015 12:39
-
-
Save keopx/3c53e278f38398d133d4 to your computer and use it in GitHub Desktop.
Drupal ajax call with values
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
| function get_data_info() { | |
| jQuery.ajax({ | |
| url: 'mymodule/api/data', | |
| type: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/x-www-form-urlencoded', | |
| }, | |
| dataType: "json", | |
| data: { | |
| 'data': data | |
| }, | |
| success: function (result) { | |
| // custom code | |
| }, | |
| error: function (error, result) { | |
| // Custom code. | |
| } | |
| }); | |
| } |
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
| function mymodule_menu() { | |
| $items = array(); | |
| //... | |
| $items['mymodule/api/data'] = array( | |
| 'page callback' => 'service_ajax', | |
| 'access arguments' => array('access content'), | |
| ); | |
| //... | |
| return $items; | |
| } | |
| function service_ajax() { | |
| $data = check_plain($_POST['data']); | |
| if (empty($data) { | |
| $return = 'false'; | |
| } else{ | |
| $return = 'true'; | |
| } | |
| drupal_json_output($result); | |
| drupal_exit(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment