I have written a Drupal module in which i am trying to call .module file via $get. It working propely.But it is returning enitire HTML page. I tried drupal_json_output since I am working on drupal -7 .I also tried using getJSON but then it stopped giving me any response at all . Please help
Here is my .module file
<?php
/**
* Implementation of hook_init().
*/
function ajax_privacy_init() {
drupal_add_js(drupal_get_path('module','ajax_privacy').'/userprivacy.js');
}
/**
* Implementation of hook_menu().
*/
function ajax_privacy_menu() {
$items = array();
$items['user/%'] = array(
'title' => 'menu privacy',
'page callback' => 'ajax_privacy_get_html', // Render HTML
'page arguments' => array(2),
'type' => MENU_CALLBACK,
'access arguments' => array('access content'),
);
return $items;
}
/**
* Callback to return JSON encoded data.
*/
function ajax_privacy_get_html($arg) {
$array['text'] = $arg;
die(drupal_json_output($array));
}
And here is my .js file
// Jquery wrapper for drupal to avoid conflicts between libraries.
(function ($) {
// Jquery onload function.
Drupal.behaviors.ajax_privacy = {
attach: function (context, settings) {
// Your JS code.
user_id_ajax=urldetect();
if(user_id_ajax!=-1)
ajax_request='user/'+user_id_ajax;
//alert(ajax_request);
$.get('',{q:ajax_request},ajaxAction);
return false;
}
};
ajaxAction=function(response){
alert(response);
};
urldetect=function()
{
current_url=window.location.href;
//alert(current_url);
user_id_temp=-1;
slash_temp=-1;
if(current_url.search("user/")!=-1)
{
user_id_temp=current_url.substring(42);
slash_temp=user_id_temp.search("/");
if(slash_temp!=-1)
{
user_id_temp=user_id_temp.substring(0,slash_temp);
}
}
return(user_id_temp);
};
})(jQuery);
// Jquery wrapper for drupal to avoid conflicts between libraries.
(function ($) {
// Jquery onload function.
Drupal.behaviors.ajax_privacy = {
attach: function (context, settings) {
// Your JS code.
user_id_ajax=urldetect();
if(user_id_ajax!=-1)
ajax_request='user/'+user_id_ajax;
//alert(ajax_request);
//try adding 'json' in your $.get call
$.get('',{q:ajax_request},ajaxAction,'json');
return false;
}
};
ajaxAction=function(response){
alert(response);
};
urldetect=function()
{
current_url=window.location.href;
//alert(current_url);
user_id_temp=-1;
slash_temp=-1;
if(current_url.search("user/")!=-1)
{
user_id_temp=current_url.substring(42);
slash_temp=user_id_temp.search("/");
if(slash_temp!=-1)
{
user_id_temp=user_id_temp.substring(0,slash_temp);
}
}
return(user_id_temp);
};
})(jQuery);
Add access callback at true :
$items['user/%'] = array(
'title' => 'menu privacy',
'page callback' => 'ajax_privacy_get_html', // Render HTML
'page arguments' => array(2),
'type' => MENU_CALLBACK,
'access arguments' => array('access content'),
'access callback' => TRUE // add this line
);
Try this :
function ajax_privacy_get_html($arg) {
$array['text'] = $arg;
print json_encode($array);
exit();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.