2012-12-18 60 views
-1

我在Joomla的AJAX連接中遇到問題。排除AJAX連接故障

AJAX

$(document).ready(function() { 
    var results = $('#hidden').serialize(); 

    var url = '<php? echo JURI::base(); ?>index.php?option=com_mls&view=list&format=raw&' + results; 

    $('#test').html(url); //Just to see that the string is working. 

    $.ajax({ 
     url: url, 
     success: function(){ 
      alert('success'); 
     }, 
     error: function(){ 
      alert('failure'); 
     } 
    }); 

}); 

的Joomla模型view=list

function ListData() 
{ 
    error_reporting(E_ALL); 
    $db =& JFactory::getDBO(); 
    $sort = JRequest::getVar('sort'); 
    $pstart = JRequest::getVar('pstart'); 
    $plimit = JRequest::getVar('plimit'); 
    $hprice = JRequest::getVar('hprice'); 
    $lprice = JRequest::getVar('lprice'); 
    $city = JRequest::getVar('city'); 
    $zip = JRequest::getVar('zip'); 
    $bdrms = JRequest::getVar('bdrms'); 
    $bths = JRequest::getVar('bths'); 

    $query = "SELECT * FROM " . $db->nameQuote('#__mls') . " WHERE 1=1 "; 
    if ($zip != null || $city != null || $bdrms != null || $bths != null || $hprice != null || $lprice != null){ 
     $firstand = "AND "; 
    } 
    $clauses = array(); 
    if ($zip != null) { 
     $clauses[] = "MSTZIP = " . $zip; 
    } 

      ... a bunch of IF statements for building query... 

    $query .= $firstand . implode(" AND ", $clauses) . $orderby . $pages; 

    $db->setQuery($query); 
    $table = $db->loadRowList(); 

    return $table; 

的Joomla查看:

function display($tpl = null) 
{ 
    $model = &$this->getModel(); 
    $array = $model->ListData(); 
    $this->assignRef('disparray', $array); 
    parent::display($tpl); 
} 

在我可以走路之前跑步,我只是想讓AJAX顯示success。不是。我無法分辨出錯在哪裏,也無法通過任何錯誤報告來幫助我。任何AJAX/Joomla精明的人都會幫忙?

+1

AJAX請求的結果是什麼?檢查Chrome中網絡檢查器的XHR選項卡 –

+2

使用您正在使用的任何瀏覽器打開開發人員工具。在Chrome中,您可以按f12或ctrl + shift + j。轉到網絡,單擊XHR以僅顯示ajax請求。您將看到請求是否正在發送以及它返回的HTTP狀態碼。首先在一個單獨的瀏覽器中測試AJAX調用的url。如果它不起作用,那麼您需要在AJAX調用工作之前檢查服務器端服務。 – Despertar

+0

使用您的開發人員工具來觀看請求/響應循環。它會顯示錯誤和其他問題。 –

回答

0

需要使用開發人員工具查看AJAX查詢字符串試圖去的地方。之後,我能夠確定URL字符串中需要訪問的正確URL和類。

//Removed '<php? echo JURI::base(); ?>' 
var url = 'index.php?option=com_mls&view=list&format=raw&' + results; 

感謝Sajjan尋求幫助。

0

嘗試以下

jQuery.post('index.php',{ 
       'option' : 'com_mls', 
       'controller': 'controllerName', 
       'task' : 'taskName', 
       'format' : 'raw',    
       'results' : results       
      }).success(function(result) { 
       alert('success'); 
      }).error(function() { 
       alert('error'); 
      }); 

代碼在控制器,你可以擁有任何你想要的(TASKNAME)的功能。如果您有任何問題,請讓我知道