jQuery ajax返回文档类型和其他我不想要的东西

|| 我试图通过ajax获取一些数据,无论出于何种原因,我都在获取doctype,标题,元数据...以及所需的数据。我想要的只是JSON数据。我正在使用joomla 1.5 我的jQuery:
jQuery(document).ready(
    function() {
        jQuery(\'#catagoryChange\').click(
            function (event) {
                event.preventDefault();
                jQuery.getJSON(\"index.php?option=com_test&task=aJax&tmpl=component\")
                    .success(function(data) {alert(data.myName); })
                    .error(function(data) {alert(\"error\"); });
            } // end of function event
        ); // end of click
    } // end of function 
); // end of document.ready
我的函数应该只回显json:
function testAxaj() {
    $json = \'
    {
        \"myName\": \"Testing\"
    }
    \';

    header(\'Content-type: application/json\');
    echo $json;
}
这就是它的回报
<!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\" xml:lang=\"en-gb\" lang=\"en-gb\" dir=\"ltr\" >
<head>
  <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />
  <meta name=\"robots\" content=\"index, follow\" />
  <meta name=\"keywords\" content=\"joomla, Joomla\" />
  <meta name=\"description\" content=\"Joomla! - the dynamic portal engine and content management system\" />
  <meta name=\"generator\" content=\"Joomla! 1.5 - Open Source Content Management\" />
  <title>Administration</title>
  <link href=\"/Test/administrator/templates/khepri/favicon.ico\" rel=\"shortcut icon\" type=\"image/x-icon\" />
  <script type=\"text/javascript\" src=\"/Test/includes/js/joomla.javascript.js\"></script>
  <script type=\"text/javascript\" src=\"/Test/media/system/js/mootools.js\"></script>


<link href=\"templates/khepri/css/general.css\" rel=\"stylesheet\" type=\"text/css\" />
<link href=\"templates/khepri/css/component.css\" rel=\"stylesheet\" type=\"text/css\" />


</head>
<body class=\"contentpane\">


        {
            \"myName\": \"Testing\"
        }

</body>
</html>
已邀请:
在我看来,这更像是一个Joomla问题。您将需要配置它根本不显示HTML模板。
尝试将php函数更改为:
function testAxaj() {
    $json = array(\"myName\" => \"Testing\");

    header(\'Content-type: application/json\');
    echo json_encode($json);
}
我想到了!我需要在回显后使用php退出功能:
function testAxaj() {
    $json = \'
    {
        \"myName\": \"Testing\"
    }
    \';

    header(\'Content-type: application/json\');
    echo $json;
    exit;
}
现在工作完美! :-)

要回复问题请先登录注册