1

I make a AJAX call from my model to the controller and instead of returning just the result of the sql query it returns a whole website (with the content of the result somewhere in the middle).

Snippet code of a function inside of my model>

echo '<li> <a onclick="nodeList('.$row->id.')">'.$row->title.'</a></li>';

JFactory::getDocument()->addScriptDeclaration("
    function nodeList(node_id){
        alert(node_id);

        jQuery.ajax({
            url: 'index.php?option=com_nautilus&task=update',
            type: 'POST',
            data: ({id: node_id}),
            success: function(data){
                console.log('success');
                console.log(data);
            },
                error: function(){
                console.log('error');
            }   
        });
    }
");

Code of my update function inside of controller.php>

public function update()
{
    $id = $_POST['id'];

    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $db->setQuery("SELECT c.title,c.id,c.type FROM #__content c INNER JOIN #__content_parents as p ON c.id=p.node_id and p.parent_id=$id");
    $result = $db->loadObjectList();
    echo (json_encode($result, JSON_FORCE_OBJECT));
}

Thank you!

1

You have to specify the output format. Otherwise, Joomla assumes Html.

For instance, working with FoF framework, you define in the Url &format=json to get a JSON rendered view.

FoF - Automatic views and web services

1

If you still want to call your ajax function placed inside the controller, without any output format then place code to exit your controller method before the ending curl bracket so that the view won't be called using the exit(); code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.