Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jqueryajax返回文档类型和一系列其他我不知道的东西';我不想要_Jquery_Ajax - Fatal编程技术网

jqueryajax返回文档类型和一系列其他我不知道的东西';我不想要

jqueryajax返回文档类型和一系列其他我不知道的东西';我不想要,jquery,ajax,Jquery,Ajax,我试图通过ajax获取一些数据,无论出于什么原因,我都会获取doctype、标题、元数据。。。还有我想要的数据。我想要的只是JSON数据。我正在使用joomla 1.5 我的jQuery: jQuery(document).ready( function() { jQuery('#catagoryChange').click( function (event) { event.preventDefault();

我试图通过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>

管理
{
“myName”:“测试”
}

我觉得这更像是Joomla的问题。您需要将其配置为完全不显示HTML模板。

尝试将php函数更改为:

function testAxaj() {
    $json = array("myName" => "Testing");

    header('Content-type: application/json');
    echo json_encode($json);
}

我知道了!我需要在echo之后使用php exit函数:

function testAxaj() {
    $json = '
    {
        "myName": "Testing"
    }
    ';

    header('Content-type: application/json');
    echo $json;
    exit;
}

现在工作完美了!:-)

很明显,它试图返回HTML。服务器端代码是什么样子的?你能创建一个只返回JSON的单独php页面吗?我可以,但我不想走这条路,我正试图坚持我的文档流的其余部分,即MVCI,但我更喜欢这种语法。更干净。@请尝试将jQuery函数更改为
$.getJSON(“index.php?option=com_test&task=aJax&tmpl=component”,[],函数(数据){alert(data.myName);})我已经弄明白了,我需要添加php函数exit。见上面我的答案。