2011-04-20 33 views
0

我想通过ajax获取一些数据,而且无论出于何种原因,我获得了doctype,标题,元数据...以及我想要的数据。我想要的只是JSON数据。我使用Joomla 1.5jquery ajax返回doc类型和其他一些我不想要的东西

我的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> 
+0

好吧,显然它试图返回HTML。服务器端代码是什么样的?你可以做一个单独的PHP页面,只返回JSON? – tjameson 2011-04-20 05:29:12

+0

我可以但我不想走这条路线,我正试图坚持我的文档流的其余部分是MVC – milan 2011-04-20 12:44:12

回答

0

我想通了!回声后需要使用php退出函数:

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

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

现在完美了! :-)

1

这看起来更像是Joomla问题。您将需要配置它不显示HTML模板。

+0

也许....我正在调查它 – milan 2011-04-20 12:44:51

0

尝试改变PHP函数:

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

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

这也不起作用,完全相同结果 – milan 2011-04-20 12:45:15

+0

虽然我更喜欢这种语法。更干净。 – milan 2011-04-20 14:34:02

+0

@milan试着改变你的jQuery函数为'$ .getJSON(“index.php?option = com_test&task = aJax&tmpl = component”,[],function(data){alert(data.myName);});' – Teneff 2011-04-20 14:38:39

相关问题