[英]Drupal $get not returning full HTML
I have written a Drupal module in which i am trying to call .module file via $get. 我写了一个Drupal模块,我试图通过$ get调用.module文件。 It working propely.But it is returning enitire HTML page.
它可以正常工作。但是它返回了整个HTML页面。 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 .
我正在尝试drupal_json_output,因为我正在研究drupal -7。我也尝试使用getJSON,但后来它根本没有给我任何响应。 Please help
请帮忙
Here is my .module file 这是我的.module文件
<?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 这是我的.js文件
// 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 : 在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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.