解决AJAX连接问题

时间:2012-12-18 17:03:41

标签: jquery ajax joomla

我在Joomla中遇到了我的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');
        }
    });

});

view=list的Joomla模型:

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精明的人都伸出援助之手?

2 个答案:

答案 0 :(得分: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)。如果您有任何问题,请告诉我

答案 1 :(得分:0)

需要使用开发人员工具来查看AJAX查询字符串的用途。之后,我能够确定需要在URL字符串中访问的正确URL和类。

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

向Sajjan求助。