2013-04-05 89 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; 
} 
相关问题