为什么drupal_json只返回页面HTML ?


Why is drupal_json only returning page 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?{'flag'=>true}

来自filefox的响应:

<!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" />
..................
..................

你的问题看起来非常类似于我如何使用Drupal返回实际的JSON ?

看起来你要用它来完成一些自动完成功能。不管怎样,函数ajax_update_node应该是这样的。我还会重命名这个函数,因为它看起来像是在实现一个钩子。这与hook_nodeapi非常相似。

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

查看http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_json_output/7并在hook_menu声明中设置一个"delivery callback"来调用它