2013-04-05 90 views
0

爲什麼drupal_json只返回頁面HTML?爲什麼drupal_json只返回頁面HTML?

下面的代碼:

PHP:

//Add js 
function update_ajax_init(){ 
    drupal_add_js("...."); 
} 

//Function hook menu of me 
function update_ajax_menu(){ 
$items = array(); 

    $items['ajax/refresh'] = array(
    'type' => MENU_CALLBACK, 
    'page callback' => 'ajax_update_node', 
    'title' => 'Ajax update' 
); 
} 

//Function main process return data json 
function ajax_update_node(){ 
    return drupal_json(array("flag"=>true)); 
} 

的Javascript:

$(document).ready(function(){ 
    $("a.update_node").click(function(){ 
     var params = {node_id: $(this).atrr("rel")}; 
     $.ajax(
      type: "POST", 
      url: "ajax/refresh", 
      data: params, 
      dataType: "json", 
      success: function(response){ 
       if (response.flag == true){ 
        alert("Success"); 
       } 
      } 
     ); 
    }); 
}); 

爲什麼響應值的所有HTML代碼,而不是JSON?從filefox {'flag'=>true}

響應:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
.................. 
.................. 

回答

1

你提的問題非常相似How can I return actual JSON using Drupal?

它看起來像你要使用它的一些自動完成功能。無論哪種方式,你的功能ajax_update_node應該是這樣的。我也會重命名這個函數,因爲它看起來像是在實現一個鉤子。這與hook_nodeapi非常相似。

function ajax_update_node() { 
    $result = array('flag' => true); 
    drupal_json($result); 
    exit; 
} 
相關問題