2011-04-20 32 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

相關問題