2015-09-08 105 views
0

我寫了一個Drupal模塊,我試圖通過$ get調用.module文件。它正在運行,但它正在返回啓用HTML頁面。我嘗試使用drupal_json_output,因爲我正在使用drupal -7。我也嘗試使用getJSON,但隨後它不再給我提供任何響應。請幫助

這裏是我的.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)); 

} 

,這裏是我的.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); 
+0

我也清除drupal緩存,如果我做任何更改 –

+0

在drupal你的輸出是json,嘗試在js文件中添加'json'關鍵字時打電話它會是這樣的$ .get(full_url,功能(數據){} 'JSON');看看你的js文件$ .get('',{q:ajax_request},ajaxAction,'json'); – vishwa

回答

0
// 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); 
+0

感謝Vishwa的迴應。但它沒有工作:( –

+0

嗨akshay我看到$ .get('',{q:ajax_request},ajaxAction,'json');沒有網址可以調用添加頁面網址爲$ .get('url',{ q:ajax_request},ajaxAction,'json');這個url必須是完整的url例子http://yoursite.com/user/1這個url將會是你在hook_menu中定義的 – vishwa

+0

進一步的解釋你可以參考以下鏈接http://www.w3schools.com/jquery/ajax_get.asp – vishwa

0

添加訪問回調:

$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 
); 

嘗試這個:

function ajax_privacy_get_html($arg) { 

$array['text'] = $arg; 
print json_encode($array); 
exit(); 
} 
+0

將嘗試此解決方案..謝謝 –

+0

好吧,讓我們知道如果這是做什麼你想:) – Fky